How to Determine the Next Available Drive Letter When Z Is Already Mapped
Script doesn't make assumptions about users' behavior
February 22, 2009
I teach a course on VBScript essentials for system administrators. When we were talking about how to detect the next available drive letter in a script that maps network drives, many administrators pointed out that the script presented in the course's textbook was problematic because it assumed that administrators always map network drive letters in alphabetical order. The script iterated through the used drive letters in alphabetical order until it found the last used letter. The next available letter was then returned. As a result, if you previously mapped a drive to the letter Z, for instance, the script informs you that there are no available drive letters even if there are because Z is the last letter in the English alphabet.
I wanted to find a VBScript solution that wouldn't assume users' behavior and would always return a drive letter if one was available. I first searched the Internet. The only script I found was discussed in the Microsoft article "Hey, Scripting Guy: How Can I Determine the Next Available Drive Letter on a Computer?" Although this script works well, the Windows Management Instrumentation (WMI) service (winmgmt.exe) must be started on the computer from which you're running the script. That's not always the case, so I decided to write a script that finds an available drive letter and maps it to a specified path. FindNextDrive.vbs is the result. Instead of using WMI to find an available drive letter, this script uses the Microsoft Scripting Runtime Library's FileSystemObject object. Thus, it works on any computer running Windows Script Host (WSH).
Here's how FindNextDrive.vbs works. The script begins by creating a Scripting Runtime Library Dictionary object (aka dictionary), which it fills with 26 key/value pairs. The keys are the letters of English alphabet. Each key's value is set to False, which indicates the drive letter is available.
Next, the script calls the GetNextDriveLetter function to find out which drives are already mapped. First, the function sets its return value to -1 in case there are no drive letters available. Then, the function creates a FileSystemObject object, uses that object's Drive property to access the collection of currently defined drives, and iterates through the collection. For each used drive letter in that collection, GetNextDriveLetter finds the equivalent drive letter (i.e., the equivalent key) in the dictionary and sets that key's value to True. For example, if the E drive is already defined, the function sets the E key's value to True, which indicates that the E drive can't be used for the new mapping. Finally, the function iterates through the dictionary to find the first key whose value is still false (i.e., a drive letter that still isn't defined) and assigns that letter to its return value.
After the GetNextDriveLetter function returns its value, the script checks it. When the return value is -1, you receive a message noting that there aren't any available drive letters. When the return value contains a letter, the script maps the letter to the specified path and displays a message noting that it just mapped that drive letter.
You can download FindNextDrive.vbs by clicking the Download the Code Here button at the top of the page. Before you run the script, you need to customize the network path you want the script to map. To specify that path, find the line
filePath = "\collegeTeachers"
and replace \collegeTeachers with your network path. Note that this script doesn't map drives using alternate credentials.
About the Author
You May Also Like