vbScript - Get Drive Letter by Volume Label

Please see my post vbScript - The Basics for more details on vbScript.

I have all my computers partitioned with two partitions. The drive letters are C (OS and applications) and D (user data). I use Macrium Reflect to take monthly full and daily differential images of C. To backup D Ii use a script front end for robocopy. Using a script to do the backup has one major problem. The drive letter of my dedicated backup external drive may change. I find that even if I have used Drive Manager to allocate a drive letter, there is no guarantee that this letter will never change.

What I can do is assign a unique volume label. Unfortunately I cannot directly use the volume label in any type of copy command. What we need is some way to scan all mounted drives and find the drive letter for a given volume label. We can do that by accessing the Drives collection returned by the FileSystemObject. If you examine the properties exposed by a single instance of a drive, for example, my D drive, via the following script:

set fso = CreateObject("Scripting.FileSystemObject")

set drive = fso.GetDrive("D")

Wscript.Echo "AvailableSpace =", drive.AvailableSpace 
Wscript.Echo "DriveLetter    =", drive.DriveLetter    
Wscript.Echo "DriveType      =", drive.DriveType      
Wscript.Echo "FileSystem     =", drive.FileSystem     
Wscript.Echo "FreeSpace      =", drive.FreeSpace      
Wscript.Echo "IsReady        =", drive.IsReady        
Wscript.Echo "Path           =", drive.Path           
Wscript.Echo "RootFolder     =", drive.RootFolder     
Wscript.Echo "SerialNumber   =", drive.SerialNumber   
Wscript.Echo "ShareName      =", drive.ShareName      
Wscript.Echo "TotalSize      =", drive.TotalSize      
Wscript.Echo "VolumeName     =", drive.VolumeName     

you get this output

AvailableSpace = 716756209664
DriveLetter    = D
DriveType      = 2
FileSystem     = NTFS
FreeSpace      = 716756209664
IsReady        = -1
Path           = D:
RootFolder     = D:\
SerialNumber   = 912945059
ShareName      =
TotalSize      = 1856350711808
VolumeName     = D-Data

By iterating through the drives collection and comparing VolumeName for all fixed drives that are currently ready we can easily determine the drive letter and proceed with the backup (or any other operation).

You'll notice that instead of doing

set fso = CreateObject("Scripting.FileSystemObject")

and then iterating through fso.Drives, I just used the object directly as it was created. For operations where you will use the object only once it is quite reasonable to do this.

One final note, accessing a drive's properties (other than the IsReady state) when the drive is not ready will result in an error. You can trap this and test for it using On Error or you can just check IsReady first.

1,899 Views
''#region Header                                                                        '
''                                                                                      '
''  Name:                                                                               '
''                                                                                      '
''      GetDriveLetter.vbs                                                              '
''                                                                                      '
''  Description:                                                                        '
''                                                                                      '
''      Given a volume label, GetDriveLetter returns the drive letter, followed by      '
''      a colon (as in D:) if the drive is currently available, or a null String        '
''      if it is not.                                                                   '
''                                                                                      '
''  Audit:                                                                              '
''                                                                                      '
''      2017-06-21  rj  Original code                                                   '
''                                                                                      '
''#endregion                                                                            '

Function GetDriveLetter (label)

    GetDriveLetter = ""

    'DriveType is one of                        '
    '                                           '
    '   0 = Unknown            3 = Network      '
    '   1 = Removable          4 = CD-ROM       '
    '   2 = Fixed              5 = RAM Disk     '
    
    Dim drive

    For Each drive In CreateObject("Scripting.FileSystemObject").Drives
        If drive.IsReady Then
            If drive.DriveType = 2 And drive.VolumeName = label Then
                GetDriveLetter = drive.DriveLetter & ":"
                Exit Function
            End If
        End If
    Next

End Function

''Test code                                                                             '
If StrComp(Wscript.ScriptName,"GetDriveLetter.vbs") = 0 Then
    WScript.Echo "Drive=",GetDriveLetter("D-Data")
    WScript.Echo "Drive=",GetDriveLetter("D-Fish")
End If
About the Author

I completed my Computer Science degree at the University of Manitoba in 1976. I did two and a half years of programming in medical research followed by twenty-nine years at Manitoba Hydro (electric utility). Most of that was spent on doing development and maintenance on an AGC/SCADA (real-time programming/process control) system. The last ten years of that was spent doing application and infrastructure support and development. I have programmed in FORTRAN (mostly), APL, PL/1, COBOL, Lisp, SNOBOL, ALGOL, Assembler (several flavours), C, C++, Paradox, VB, vbScript and more recently, Python. I am married with two grown children of whom I am very proud, and a most beautiful wife. I am currently retired (and loving it).