Developer

Studying the Map Local Drive script

You asked for it, now here it is. Greg Shultz introduced you to his local drive mapping script in an earlier Daily Drill Down. Here, he covers each line in detail so you can get a handle on how this handy utility works.


In my previous Daily Drill Down, I introduced you to a VBScript that allows you to map your local folders. I also included the completed script as a download for those of you who do not want a lesson on scripting. Regardless of whether you actually typed in the Map Local Drive script or opted to download it, it’s worth the time to take a step-by-step look at the VBScript commands in the script. Doing so will help you gain a better understanding of how this script works, as well as provide you with some programming techniques, particularly in the area of using subroutines and functions. You can then use these on any script that you develop.

To help you gain a better appreciation of the point of breaking up a script into subroutines and functions, I’ll go through each of these components separately in this Daily Drill Down. Then, when I take a look at how the script works, you’ll see how these components work together to accomplish the task at hand.

Note
To get the full benefit of this lesson, it is a good idea to refer back to my earlier Daily Drill Down, in which each line of code is numbered. I’ll make several references to these line numbers throughout this Daily Drill Down.

The driver section
Lines 1 through 25 make up the main section, or the driver of the script. Lines 1 through 3 use the Dim statement to define and allocate space in memory for the variables that the script will use. As you can see, I’ve opted to use variable names that more closely represent the information that the script will store in the variables, rather than cryptic variable names that are typically used in VBScript. This makes it easier to keep track of exactly what the variable is being used for. For instance, the WhatToDo variable stores the choice you make from the opening menu—indicating to the script what you want to do.

The next two lines use the Set statement to assign object references to variables. Once that occurs, you can use the variables to access the methods that the objects make available. Line 4 uses the Set statement to activate all of the features of the Windows Script Host object model and assign those features to the WshShell variable. Since this script will need to access the file system in order to map drive letters, line 5 uses the Set statement to activate all of the file system features provided by the FileSystemObject and assigns them to the FileShell variable.

Lines 6 through 10 create the script’s main menu, which is then assigned to the MapPrompt variable. To format the menu appropriately, I use the vbCrLF constant, which issues a carriage return-linefeed combination, to separate the lines. The & (ampersand) symbol is the concatenation operator and is used to join two separate items. The _ (underscore) symbol is the line continuation character and indicates that the line that precedes it and the line that follows it should be considered part of the same line. By using the vbCrLF constant with the & and _ symbols, I can create a nicely formatted prompt on five separate lines, trick VBScript into treating them as one line, and assign it to a single variable—MapPrompt.

Lines 11 through 23 form a loop that will continually display the main menu until the user either selects the Quit option or clicks the Cancel button. Line 12 uses the InputBox function to display the menu from the MapPrompt variable. When the user makes a selection from the menu and clicks OK or clicks Cancel, that value is assigned to the WhatToDo variable.

To determine which selection the user made, lines 13 through 22 set up a Select Case statement that analyzes the selection and launches the appropriate subroutine or terminates the script. To launch the appropriate subroutine, the script uses the Call statement, which is designed to transfer control of the script’s execution path.

Lines 19 through 21 make up the error handling section of the Select Case statement. If the user types any character other than 1, 2, 3, or 4, the script displays an error message and the loop runs again, redisplaying the menu so the user can essentially try again.

The Mapping subroutine
Lines 25 through 46 make up the Mapping subroutine. Basically, the job of this subroutine is to get a valid drive letter, prompt the user to select a folder, and then map the drive letter to that folder using the DOS-based Subst command. Let’s take a closer look.

Line 25 uses the Sub statement, along with the name, to declare or delineate the beginning of the Mapping subroutine. Line 26 calls the GetDrvLetter function and sends it the word “Map,” which is used to indicate to the GetDrvLetter function that this particular call is coming from the Map subroutine. I’ll explain more about the GetDrvLetter function in a moment, but basically it’s designed to return a drive letter, which is then appropriately assigned to a variable called DrvLetter.

In line 27, the script calls the DrvExists function and sends it the value stored in the DrvLetter variable. Again, I’ll go into more detail on the DrvExists function later, but it basically determines whether the selected drive letter is already in use and returns a Boolean value of either True or False, which is then assigned to the TestDrvLetter variable.

Lines 28 through 45 make up two If…Then…Else statements, one nested inside the other, which I use to determine how the Mapping subroutine works. In line 28, the script determines whether the value returned by the DrvExists function is True or False. If the value in the TestDrvLetter variable is True, then line 29 and 30 create and display an error message using the MsgBox function. If the value in the TestDrvLetter variable is False, the drive letter is available and the script continues execution on line 32. This line uses a Set statement to assign the Application object reference to the BrowseDialogBox variable. The Application object reference refers to the methods that the Active Desktop feature makes available, which include the standard Browse For Folder dialog box.

Once the script activates the Application object reference, it can then access a standard Browse For Folder dialog box. To do so, line 33 uses the BrowseDialogBox variable to access the BrowseForFolder method, which will then display a Browse For Folder dialog box you can use to locate and select the folder to which you want to map a drive letter. Once you select a folder and click OK, the BrowseForFolder method will then return the name of that folder and assign it to the SelectedFolder variable.

Now, lines 34 through 44 make up the nested If…Then…Else statement that performs several important functions. Line 34 tests to see whether you’ve actually selected a folder in the Browse For Folder dialog box and clicked OK or clicked the Cancel button. If you clicked the Cancel button, the script calls the Windows Script Host’s Quit method to terminate the script.

If you selected a folder and clicked OK, line 37, which begins the Else portion of the statement, assigns the object, which contains the full path to the folder you selected in the Browse dialog box, to the FullPath variable. However, the script can’t really access the full path to the folder when it’s stored as an object. Line 38 uses the CStr function to convert the object into a string and assigns it to the FullPathString variable.

At this point, I create the Subst command line as a string and assign it to the DrivePath variable in line 39. To do so, I use the & concatenation operator to combine the Subst command with the selected drive letter and the path to the folder. To separate the drive letter from the path, I also use the & concatenation operator to add a blank space between the variables.

Because the path could include long folder names, it must be enclosed in double quotes in order for the Subst command to work properly. I use the & concatenation operator to append four double quote marks before and after the FullPathSting variable. While you might think that you’d only need to use three double quote marks to achieve this, you have to use four to prevent the concatenation operator from being included in the string that makes up the path.

Now, in line 40, I use the Run method of the WshShell object to actually run the Subst command and map the drive letter to the folder. Lines 41 and 42 then open Windows Explorer to display the new mapped drive. In line 43, I set the WhatToDo variable to 4, which causes the loop in the main section of the script to exit and subsequently terminates the script.

The Disconnecting subroutine
The Disconnecting subroutine is made up of lines 47 through 60. This routine’s job is to prompt the user to select the mapped drive to disconnect and then run the Subst command with the delete parameter.

In line 48, the script calls the GetDrvLetter function and sends it the word “Disconnect,” which is used to indicate to the GetDrvLetter function that this particular call is coming from the Disconnecting subroutine. The result is then assigned to the DrvLetter variable.

Line 49 tests the DrvLetter variable for special cases in which there are no mapped drive letters to disconnect. If that is the case, the GetDrvLetter function returns a value of 0 rather than a drive letter and the Exit Sub statement immediately terminates the Disconnecting subroutine. At this point, execution of the script returns to the driver section of the script.

In line 50, the DrvLetter variable is sent to the DrvExists function. That function determines whether the selected drive letter is already in use and returns a Boolean value of either True or False, which is assigned to the TestDrvLetter variable.

Lines 51 through 59 make up an If…Then…Else statement that determines whether the value returned by the DrvExists function is True or False and runs the appropriate set of commands. If the value in the TestDrvLetter variable is True, the drive letter exists and lines 52 and 53 create and run the Subst command with the delete parameter. Lines 54 and 55 then create and display a confirmation dialog box. I use the WshShell.Popup method to display the dialog box because it allows me to configure it to close itself after a set period of time. In this case, I’ve configured the dialog box to close after displaying its message on the screen for five seconds. This message is simply an acknowledgment and doesn’t require the user to do anything. Because the Popup method is actually designed to return a result, I must assign that result to the Dummy variable, even though the script will never use it.

Now, if the value in the TestDrvLetter variable is False, the drive letter doesn’t exist and lines 57 and 58 create and display a dialog box using the MsgBox function that alerts the user to the problem.

The Viewing subroutine
The Viewing subroutine is very short, made up of only five lines. This subroutine’s job is simply to display any mapped drive letters that exist. To do so, line 62 calls the ViewMappedDrvs function, which compiles a list of mapped drive letters, if any, and stores the results in the TheLetters variable. The value stored in the TheLetters variable will either contain a string listing the mapped drive letters or a string indicating that there are no mapped drive letters. Lines 63 and 64 use the MsgBox function to create and display a dialog box that lists the value stored in the TheLetters variable.

The GetDrvLetter function
Lines 66 through 86 make up the GetDrvLetter function, which uses a nested If…Then…Else statement to determine which subroutine called it and then to prompt the user to enter a drive letter. Line 67 tests whether the value stored in the MapDis variable is the string Map, which indicates that the call came from the Mapping subroutine. If it is, lines 68 and 69 display an Input dialog box that prompts the user to enter a drive letter and assigns the result to the TempDrvLetter variable.

If the string value in the MapDis variable is Disconnect, line 71 calls the ViewMappedDrvs function, which compiles a list of mapped drive letters, if any, and stores the results in the TheLetters variable. If the ViewMappedDrvs function determines that there aren’t any mapped drives, then the If statement in line 72 is True and the MsgBox function in line 73 displays that message. Since there’s no need to continue, line 74 assigns the GetDrvLetter function the special case value of 0 and line 75 exits the function. As you’ll remember, when the GetDrvLetter function returns the special case value of 0 to the Disconnecting subroutine, it, too, terminates and control is then returned to the driver section of the script.

Now, if the TheLetters variable does contain a list of mapped drive letters, lines 77 through 79 create and display an appropriate prompt using the InputBox function. When the user selects a mapped drive, that value is then stored in the TempDrvLetter variable.

Execution then drops out of the nested If…Then…Else statements and line 82 calls the IsValidDrvLetter function and sends it the drive letter value stored in the TempDrvLetter variable. If the IsValidDrvLetter function determines that the value stored in the TempDrvLetter variable is indeed a valid drive letter, the next two lines test to see if the user typed a colon along with the drive letter. To perform this test, I use the Right function to retrieve the right-most character in the string and then assign it to the ColonTest variable in line 83. In line 84, I test to see whether the colon is there. If it isn’t, I use the concatenation operator to add it to the drive letter. Finally, in line 85, the script assigns the drive letter stored in the TempDrvLetter variable the function name, which then returns the selected drive letter to the calling subroutine.

The IsValidDrvLetter function
As you’ve probably surmised by now, the job of the IsValidDrvLetter function, lines 87 through 97, is to determine whether the value stored in the TempDrvLetter variable is indeed a valid drive letter. To begin with, line 88 tests to see if a value exists in the TempDrvLetter variable. If the user clicked the Cancel button when prompted to enter a drive letter, the value will be an empty string and the Wscript.Quit method will terminate the script.

If the TempDrvLetter variable does contain a value, line 89 uses the UCase function to convert a lowercase character into uppercase. Line 90 then uses the Asc function to determine the ASCII value of that character and assigns it to the IsLetter variable.

Lines 91 through 96 make up an If…Then…Else statement that determines which character in the IsLetter variable is a valid choice for a drive letter. Line 91 determines whether the value stored in the IsLetter variable is between 65 and 90, which would correspond to the letters A through Z. If it is, line 92 assigns the drive letter stored in the TempDrvLetter variable to the function name, which then returns the selected drive letter to the calling function. If the ASCII value of the character is less than 65 or greater than 90, then it isn’t a valid choice for a driver letter, and the Wscript.Quit method will terminate the script.

The DrvExists function
Lines 98 through 104 make up the DrvExists function. The job of this function is to simply determine whether the drive letter the user typed is already in use and return a value of True or False to the calling subroutine, depending on the result of the test. To make the test, the script uses a simple If…Then…Else statement, whose main component is a call to the DriveExists method of the FileSystemObject via the FileShell object variable.

If the drive letter is already being used by a drive, it obviously can’t be used again and the function name is assigned a value of True. If the drive letter isn’t currently assigned to a drive in the system, then it is available and the function name is assigned a value of False.

The ViewMappedDrvs function
The last function in the script is ViewMappedDrvs on lines 105 through 118. The job of this function is to simply get a list of the drives currently mapped by the Subst command. To do so, I use the Run method in line 106 to shell out to DOS via the %COMSPEC% /c command. COMSPEC is an environment variable that translates into the command interpreter for the specific operating system (Cmd.exe in Windows NT and Windows 2000 or Command.com in Windows 95, Windows 98, or Windows Me). Here, COMSPEC starts a DOS session and the /c parameter configures the command interpreter to carry out the commands that follow it and then exit. In this case, the command that follows it is a Subst command, along with the DOS-based redirection symbols (>) to pipe the results of the Subst command into the Subst.txt file.

In line 107, I use the Windows Script Host Sleep method to force the script to pause long enough for the Subst file to be created. Line 108 then uses the FileSystemObject’s GetFile method to access the file from within the script. I then test the size of the file. If there are no mapped drives, the Subst.txt file will be empty and will have a file size of 0. In that case, the script assigns the text string "    There are no mapped drive letters!" to the function name, which is then returned to the calling subroutine or function. You’ll notice that the string contains four leading spaces. This allows the string to be displayed nicely in a dialog box.

If the file size is larger than 0, the file contains a list of mapped drive letters, and I use the OpenTextFile method in line 112 to open the Subst.txt file for reading. The script then reads the entire contents of the file as a string and assigns it to the function name and the contents are then returned to the calling subroutine or function. Lines 114 through 116 then close the file and delete it.

Using the Map Local Drive script
Now that you have a good idea of how the Map Local Drive script works, let’s take a look at how you would use it. When you first run it, you’ll see the menu dialog box and will select one of the four options by typing a number in the text box and clicking OK. If you select the Map Local Drive option, you’ll be prompted to select a drive letter and then a folder. These steps are illustrated in Figure A. When you click OK in the Browse For Folder dialog box, Windows Explorer will launch and display the contents of the newly mapped local drive.

Figure A
When you want to map a drive letter to a local folder, these are the steps that the Map Local Drive script will walk you through.


If you select the Disconnect Mapped Local Drive option, you’ll be prompted to select the drive letter that you want to disconnect. When you type a drive letter and click OK, you’ll see a confirmation message box. These steps are illustrated in Figure B. The confirmation dialog box will remain on the screen for five seconds and then automatically close.

Figure B
When you want to disconnect a mapped drive letter, these are the steps that the Map Local Drive script will walk you through.


If you simply want to see which drive letters are already mapped, you’ll select the View Mapped Local Drives option. When you do, you’ll see a message box that lists the mapped drives. These steps are illustrated in Figure C.

Figure C
When you select the View Mapped Local Drives option, you’ll see a message box that lists the mapped drives.


Conclusion
That’s it! With a few clicks, you are on your way to mapping any folder on your system. Considering the lengthy command-line entries you would have to make using the Subst command, this script is a real time-saver. Just don’t get too carried away, as there are only so many letters you can use before you start forgetting which letter goes with which folder.

About

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

Editor's Picks

Free Newsletters, In your Inbox