VBScript: Ensure Backslash Folder Path

To preface what could be regarded as a very small thing, I am reminded of a common turn of phrase concerning the amount of perspiration that should be afforded to the "stuff" belonging to a particular size-category, and pretty sure there was a book written about that topic.

Not to come across as disagreeable, but rather to invite a little balance based on my own general observations over time, an increase in cost with things such as time, money, even relationships, can be avoided when the "small stuff" is given the regard that it deserves.


One of these tiny details has to do with string variables that reference folder paths within a script. There are times when it is important that a defined directory for a particular folder should and must always end with a backslash. As an example:

folderPath = "C:\Example" 


Of course, it really is not very difficult to manually enter that backslash into the path.

folderPath = "C:\Example\" 


OK, that was pretty simple. If the script is being maintained by the author and always run from the same location, then there's really nothing more to do. Suppose the script is copied over to a new location to be reused or shared. In that case, portability needs to be considered.


Sharing Scripts

When building a script file, it is usually a good idea to place any constants or variables near the top so that the working code below can be left alone. When scripts are shared amongst friends or colleagues, these definitions can be easily modified to suit the new environment.


Perhaps further down in the script where the real work takes place, that variable is used in such as way that a failure would occur if the folderPath did not have a backslash. So, once that reference is modified, we need to ensure that the path has a backslash. As an example:

folderPath = "C:\Example\" 
fileName = "Test.txt" 
fullFilePath = folderPath & fileName 


The fullFileName value would now be "C:\Example\Test.txt" by concatenating both the folderPath and fileName variables. Now consider the following where the folderPath variable is modified, but the person making the change does not include that needed backslash:

folderPath = "C:\Target" 
fileName = "Test.txt" 
fullFilePath = folderPath & fileName 


As a result of this innocent modification, the string value of fullFileName is now "C:\TargetTest.txt" and creates an issue requiring some troubleshooting, either near the top where the issue resides, and possibly draw unnecessary attention into the working section of the script.


How could such a seemingly trivial problem be handled?


Approach 1 - Add Commented Instructions

Adding useful comments to code is very simple to do, and truly is a great thing. In VBScript or VB.NET, you can use the apostrophe (') character at the beginning of the line to comment out that line so it is ignored by the compiler.

' make sure folderPath ends with a backslash and 
' the string is enclosed in double-quote marks 
folderPath = "C:\Example\" 
fileName = "Test.txt" 
fullFilePath = folderPath & fileName 


The only real issue with this approach is not with the comments themselves, but that it assumes the person making that "tweak" must read them and comply. You might be thinking that the person making the adjustment should just pay attention, and you would have my agreement.

It would be unfair to assume that the only person making the desired alteration to the target variable would be someone other than its creator. An author revisiting code from many moons ago may not possess the same top-of-mind clarity once had during build-time, so comments are always good.


Approach 2 - Add the Backslash to the Final Variable

For the fullFileName variable, it is simple enough to make a change where the backslash binds the folderPath and fileName variables together.

folderPath = "C:\Example" 
fileName = "Test.txt" 
fullFilePath = folderPath & "\" & fileName 

fullFilePath = "C:\Example\Test.txt" 


The desired result is there, until a backslash is added the folderPath value, and the output value is no longer formatted properly.

folderPath = "C:\Example\" 
fileName = "Test.txt" 
fullFilePath = folderPath & "\" & fileName 

' fullFilePath = "C:\Example\\Test.txt" 


As a note, there have been instances where those extra backslashes did not prevent the script from working, but aside from the fact that it's a little sloppy, it is best to format the output to match what is expected by the variable's consumer.


Approach 3 - Custom BackshashPath Function

Wouldn't it be nice if there was no need to concern ourselves as to whether or not the script-modifier remembers to add that single, simple backslash to the end of the folder path. We can create a simple function to ensure that the folderPath has a backslash.

folderPath = "C:\Example\" 
fileName = "Test.txt" 
fullFilePath = BackslashPath(folderPath) & fileName 

' fullFilePath = "C:\Example\Test.txt" 

' add a trailing backslash to the end of the string 
Function BackslashPath(folderPath)
    If Right(folderPath, 1) = "\" Then 
        BackslashPath = folderPath 
    Else 
        BackslashPath = folderPath & "\" 
    End If
End Function 


The BackslashPath() function works by evaluating the last character of the folderPath value. There is one input parameter for the function to take in the folderPath.

Function BackslashPath(folderPath)


If the folderpath has a backslash character, then the folderPath value itself is left alone by assigning it as the return value of the function. Otherwise, the backslash character gets tacked on right to the end of the folderPath and returned.



In order to use this function, it can be placed as part of the fullFilePath value assignment where the folderPath would be positioned using the folderPath variable as the function's input.

fullFilePath = BackslashPath(folderPath) & fileName 


Taking this approach means indifference to the exact nature of the assignment of the folder path. In a way, the BackslashPath() function behaves like a filter for the input, and increases the overall reliability and portability of the script to execute on the intended outcome.


In Closing

As a matter of practice, there are circumstances when adding more lines of code to a script can reduce complexity, and actually make things more simple. In this case, something as "small" as handling a backslash with a custom BackslashPath() function can be one less thing to "sweat" about.

Comments

Popular posts from this blog

AutoStore: Create a Custom RRT

AutoStore: Workflow Loop Example