Handling Variable USB Drive Letters for Backups

Backups on USB disks may be tricky due to a weird problem we seem to be doomed to live with on Windows: variable drive letters.

Robocopy is an example of a smart and free tool for USB backups, but, if the USB devices you have around are more than one, you’ll have to handle variable drive letters.

You can have multiple versions of your scripts – one for each possible letter – but more flexible solutions can be there, using volume names.

One possibility relies on the powerful yet little known Visual Basic Script, a scripting language ways more powerful than the good old batch files. Exploiting it for simple tasks is much less difficult than it may seem, especially given the countless examples on the Internet.

A VBScript – like a batch file – is just a text file, with .vbs extension, written in a simple scripting language similar to Visual Basic. Windows recognizes it with no other requirements. Double click on it and it runs.

In the script proposed here, we’ll find three parts. One is the function that converts a volume name in the letter, the second finds the actually connected letter, and the third is the backup command.

Let’s assume that you use two drives, for alternate backups, named BACKUP1 and BACKUP2.

' 1. from volume name to letter
Function GetDriveLetter (label)
    GetDriveLetter = ""
    Dim drive
    For Each drive In CreateObject("Scripting.FileSystemObject").Drives
        If drive.IsReady Then
            If drive.VolumeName = label Then
                GetDriveLetter = drive.DriveLetter & ":"
                Exit Function
            End If
        End If
    Next
End Function

' 2. get the letter
set wshShell = CreateObject("WScript.Shell")
dim Letter
if GetDriveLetter("BACKUP1")<>"" then
	Letter=GetDriveLetter("BACKUP1")
elseif GetDriveLetter("BACKUP2")<>"" then
	Letter=GetDriveLetter("BACKUP2")
else
	msgbox "Drive not found."
	WScript.Quit
end if

' 3. backup
command = "cmd /k robocopy ""C:\My folder"" """ & Letter & "\My folder copy"" /mir"
wshShell.run command

The script will run the backup command for the connected drive. Just, if you connect both drives at the same time, BACKUP1 will be given the priority and used.

The upside of this script if that you have only one script for all of your backup drives. It detects the disks independently of their drive letter, and it automatically runs your backup on the currently connected drive.

The script is, of course, just an example, even if working. In particular, you may want to elaborate on the robocopy command.