Iterating a File for Variable Assignment in VBScript

VBScript is a daunting scripting language that can seem very inexplicable at times. The language has a very unstructured approach to scripting and seems to be a little messy at first. Initially, I didn’t enjoy using it, but over time it seemed to grow on me because of the simplicity it yields with executing PowerShell commands specifically related to automating basic system tasks.

One issue I recently had to overcome was global variable assignments to help track the status of certain things.

Originally, I was using setx to set global variables to support my requirements. However, I quickly realized that setting variables in this manner doesn’t exactly update in the current environment. I ended up ditching setx in lieu of writing the variables to a file. The new issue was getting these from a text file into VBScript variables that can be evaluated.

Since I was working with multiple variables, the easiest solution was to read the file and iterate through each line assigning the variables based on their line value/parameter assignment.

First, I had to get the variables into the file. For this example, I will keep it simple with 3 system variables; I will be using the command Test-Path to determine if 3 folders exist and I will be appending the results to a file using Out-File

$var = Test-Path -Path Videos; $appendvar = 'videos:' + $var|Out-File -append -FilePath example.txt

$var = Test-Path -Path Music; $appendvar = 'music:' + $var|Out-File -append -FilePath example.txt

$var = Test-Path -Path Audio; $appendvar = 'audio:' + $var|Out-File -append -FilePath example.txt

If you check the same folder, you will see example.txt was created. Assuming you ran this in the default PowerShell directory (which is C:\Users\USER). The file contents should be as follows:

videos:True music:True audio:False

I needed to get all of the basics declared and initiated; In the same folder, create a test.vbs…

Now, iterating through the file is simple. With the file open, we want to split the results of the file by the new line character (in the case of Windows, **CRLF **is the default new line). In VBScript, the split operation will take the file as the first argument and the delimiter as the second (vbCrLf is the variable for the CRLF). The split operation will break the variable into an array by default.

Next, we will loop through the results and split again, but this time, we will take the original variables from results and assign them to the array based on a case statement.

As you can see, I set my for loop to cycle 3 times (the size of arr declared at the beginning) and I step through each number (Step 1). As we step through the loop, I take the first slot of results (based on the loop’s i value) and I split that based on the colon ( : ). Simply taking a look at the example.txt, you’ll see that the commands executed to populate the file added the title for us to search and the result of the command. For instance, we were looking to see if the Videos folder exists; it created videos: and then appended the results of the operation; True. So, results(0) value is videos:True.

That variable is assigned to x and then y is created by splitting x into two parts (another array). The redundant assignments aren’t necessary, but do help in explaining. Looking at how results was broken down; y is an array that is broken into two parts with the : being the delimiting character. So, y(0) is videos and y(1) is True. This is how we get the variable needed.

Finally, we are looking at y(0) to determine how we set the arr(*) value. We can then cycle through arr to assign to other variables. (Ideally, you would have declared your variables like videosFolderExist, musicFolderExist, etc… and instead of the array, just update the case based on the requirements).

Ultimately, using the arr array, the results would look something like this: