How Can I Determine the Next Available Drive Letter on a Computer?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the next available drive letter on a computer?

— TW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TW. Interesting question, and one that crops up from time-to-time. In the old days, when users only had drives A, B, and C, this was easy; you could pretty much guess that any drive letter other than those three would be available. Nowadays, though, things are different: not only does increased networking means it’s more likely that users already have a drive or two mapped, but the proliferation of CD-ROM and DVD drives, USB drives, portable hard drives and other similar devices almost ensures that a computer has more than just drives A, B, and C. So in the midst of this high-tech alphabet soup, how can you determine which drive letters are available and which ones aren’t?

Actually, this is easier than you might think. To begin with, there are only 23 letters we need to worry about; we can forget about drives A, B, and C. (Drives A and B, of course, are reserved for floppy disks, and the first hard drive in a computer will always be drive C.) To find the next available drive letter, we can start with drive D, then drive E, then drive F, continuing down the line until we either find an available letter, or until we reach drive Z.

Of course, to do that, we also need to know which drive letters are in use on a computer. Fortunately, that’s really easy: we can return a collection of in-use drive letters by using the Win32_LogicalDisk class and checking the DeviceID property. Thus:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colDisks = objWMIService.ExecQuery(“Select * from Win32_LogicalDisk”)

For Each objDisk in colDisks Wscript.Echo objDisk.DeviceID Next

On our test computer, we get back the following:

C:
D:
E:
G:
Z:

Obviously we can look at this data and easily determine the next available drive letter. But why should we have to do all that work? Didn’t someone say something about getting the script to determine the next available drive letter?

You bet we did. Here’s a script that can determine the next available drive letter on a computer. Let’s take a look at the script, and then we’ll explain how it works:

Set objDictionary = CreateObject(“Scripting.Dictionary”)

strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colDisks = objWMIService.ExecQuery(“Select * from Win32_LogicalDisk”)

For Each objDisk in colDisks objDictionary.Add objDisk.DeviceID, objDisk.DeviceID Next

For i = 67 to 90 strDrive = Chr(i) & “:” If objDictionary.Exists(strDrive) Then Else Wscript.Echo strDrive & ” is the next available drive letter.” Wscript.Quit End If Next Wscript.Echo “There are no available drive letters on this computer.”

We start out by creating an instance of the Dictionary object, a scripting object that makes it easy to store data in memory and – equally important – makes it easy to determine which pieces of data are stored in memory and which ones aren’t. (For a more detailed discussion of the Dictionary object, see the Scripting Runtime Primer in the Microsoft Windows 2000 Scripting Guide.) We then use the Win32_LogicalDisk class to retrieve a list of all the drive letters (DeviceIDs) currently in use on the computer. However, this time we’re not going to echo the information back to the screen; instead, we’re going to store it in our Dictionary object. That’s what this line of code does:

objDictionary.Add objDisk.DeviceID, objDisk.DeviceID

Elements in a dictionary are composed of two parts: the key and the item. For this particular script we really only need the key (which provides a way for us to quickly determine whether information exists in the dictionary), but the Dictionary object requires both pieces. Therefore, we simply set both the key and the item to the drive letter; hence the identical objDisk.DeviceID parameters passed to the Add method.

When the script finishes looping through the collection of drive letters, we’ll have a dictionary consisting of the following keys (and items):

C:
D:
E:
G:
Z:

Now that we have a list of all the drive letters currently in use on the computer, all that’s left to do is to find the next available drive letter. That’s what this block of code does:

For i = 67 to 90
    strDrive = Chr(i) & “:”
    If objDictionary.Exists(strDrive) Then
    Else
        Wscript.Echo strDrive & ” is the next available drive letter.”
        Wscript.Quit
    End If
Next
Wscript.Echo “There are no available drive letters on this computer.”

Don’t worry; we’ll explain it. As we noted earlier, we need to check letters D through Z to see if any of these babies are available for drive mapping. A really easy way to do this is to create a For Next loop that loops through the ASCII values of these letters. As it turns out, the ASCII value of an uppercase D is 67 and the ASCII values of an uppercase Z is 90. Thus our For Next loop runs from 67 to 90:

For i = 67 to 90

ASCII values are perfect for a For Next loop, but not so perfect for working with drive mappings (very few computers have, say, a drive 81:). Therefore, in our next line of code we use the Chr function to convert the ASCII value back to its character equivalent. For example, when the loop first runs, i (our loop variable) will be equal to 67. The Chr function will take this value (67) and convert it to an uppercase D. We tack a colon on the end, and our variable strDrive will then hold the value D:, something which looks an awful lot like a drive letter.

The rest is child’s play. (Well, assuming your child knows something about VBScript.) We use the Dictionary object’s Exists method to see if the first drive letter – D: — exists in the dictionary. Remember, the dictionary contains a list of all the drive letters that are not available for use. If D: is found in the dictionary, then that means a drive is already using that letter. In that case, we loop around and try the next drive letter (E:).

What happens if we find a drive letter that isn’t in the dictionary? Well, that means that the drive letter is available to us. So, we do two things. First, we echo a message indicating that this drive letter is available. Second, we use the Quit method to exit the script. Why exit the script at this point? Well, we’ve found the next available drive letter, and that’s all we wanted; there’s no reason to keep checking letters. So we exit and get on with our lives.

And what if no drive letter is available? If that happens, we leave the For Next loop and then run the last line of the script, which simply says that no drive letter is available. Because we exit the script as soon as an available drive letter is found, this line of code executes only if our search for a drive letter has failed.

0 comments

Discussion is closed.

Feedback usabilla icon