Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


February 23, 2009

How to Determine the Next Available Drive Letter When Z Is Already Mapped

Script doesn't make assumptions about users' behavior
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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 = "\\college\Teachers" 

and replace \\college\Teachers with your network path. Note that this script doesn't map drives using alternate credentials.

 

End of Article



Reader Comments
I know it's cheating but I can't help it. :-)

If you aren't concerned about what drive letter you get you could always shell to a

NET USE * \\servername\share

The asterisk * will grab the next available drive letter.

Like I said, cheating but...

vergeh April 07, 2009 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Is Microsoft Just Like IBM?

Microsoft has defined the way we do business from a technology perspective for years. But with a younger generation that lives in the clouds, is Microsoft's recent progress in cloud computing too little, too late? ...

Microsoft, News Corp. Discuss Locking Out Google

Microsoft and Rupert Murdoch's News Corp. recently discussed an alliance that would counter Google's fledgling online news service. ...


Related Articles Dictionaries Trump Arrays

The Scripting Dictionary Makes It Easy

Understanding VBScript: The Drive Object

Understanding VBScript: Manipulating Files with FileSystemObject

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

7 Ways To Get More From Your SharePoint Deployment Now

Configuration Manager SP1 and R2 Overview

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement