So I made a couple of config files and thought there MUST be a better way

So I created a script that has 2 modes, Live, and testing. It also has a lot of different paths to files to keep track of (Around 32) that change depending on the scripts mode. So really…64 Paths (32 for live and 32 for testing).

I have 2 directories that keep these data files. A Live directory and a test directory. They are both mirrored. I know, I know it would be best to have a testing environment but I don’t have that luxury at work so I create my own testing environment on the computer I have access too.

Finally I have 2 config files (CSV files actually) The config file that tells the script it’s in Live mode or Testing Mode is named “ScriptConfig.csv” because it has other things in there too. The Columns are:

"Item","Enabled","Mode" 

the line that instructs the script as to what mode it’s in looks like this:

"Script","-","Live" 

I change Live to Testing if I want the script to be in testing mode.

The config file that has all the paths is named “PathConfig.csv and the Columns are:

"Mode","Item","Path" 

Now, there are 64 Lines in that file but to keep this post short I’ll just list 4 generic ones which look like this:

"Live","Item1","c:LiveexampleSubDir1example1.txt" "Live","Item2","c:LiveexampleSubDir2example2.txt" "testing","Item1","c:TestexampleSubDir1example1.txt" "testing","Item2","c:TestexampleSubDir2example2.txt" 

Now, here is the part that I think could be made simpler…The code that assigns variables a path. The variable names are the same as the name under the “Item” column in the PathConfig.csv file (To keep it simple)

$PathConfigPath = 'c:LiveExampleConfigPathConfig.csv' $ScriptConfigPath = 'c:LiveExampleConfigScriptConfig.csv' $Global:PathConfig = Import-Csv $PathConfigPath $Global:ScriptConfig = Import-Csv $ScriptConfigPath #This is the part that assigns variables the correct Path based on the mode defined in ScriptConfig.csv $ScriptConfig | ForEach-Object { if ($_.Item -eq "Script" -and $_.Mode -eq "Live") { $PathConfig | ForEach-Object { if ($_.Mode -eq "Live" -and $_.Item -eq "Item1") {$Global:Item1 = $_.Path } } $PathConfig | ForEach-Object { if ($_.Mode -eq "Live" -and $_.Item -eq "Item2") {$Global:Item2 = $_.Path } } } $ScriptConfig | ForEach-Object { if ($_.Item -eq "Script" -and $_.Mode -eq "Testing") { $PathConfig | ForEach-Object { if ($_.Mode -eq "Testing" -and $_.Item -eq "Item1") {$Global:Item1 = $_.Path } } $PathConfig | ForEach-Object { if ($_.Mode -eq "Testing" -and $_.Item -eq "Item2") {$Global:Item2 = $_.Path } } } 

So, as you can see, in order to set each variable I am parsing the $PathConfig array like…ALOT!! I mean, it’ll work but, it will take a little time and alot of “Code space so to speak” there has to be a more simpler way that requires less code and take less time.

If anyone can come up with a simpler way while still using config files (There is a reason I need to use config files), that would be great!!

EDIT: I tried formatting the code with 4 spaces at the start of each line of code…sorry if it didn’t work?

submitted by /u/neotrin2000
[link] [comments]

Leave a Reply