3.13. Creating a Virtual Drive to Another Drive or Folder

Problem

You want to make a folder the root of a drive or you want to use multiple drive letters for the same drive.

Solution

Using a command-line interface

Use the following command to create a new drive pointing to an existing path on the system:

> subst <Drive> 
                  <Path>

The following example creates an E: drive pointing to C:\scripts:

> subst E: C:\scripts

The following example creates an F: drive pointing to C:

> subst F: C:\

Using VBScript

There aren't any WMI or WSH interfaces for creating virtual drives, but you can shell out to the subst command if you really want to do it via a script.

' This code creates a virtual drive.
' ------ SCRIPT CONFIGURATION ------
strDrive = "<Drive>"  ' e.g., e:
strPath  = "<Path>"   ' e.g., c:\scripts
   
' This assumes subst is in your PATH, if not, fully qualify
' the path to the command here:
strCommand = "subst " & strDrive & " " & strPath
' ------ END CONFIGURATION ---------
set objWshShell = WScript.CreateObject("WScript.Shell")
intRC = objWshShell.Run(strCommand, 0, TRUE)
if intRC <> 0 then
   WScript.Echo "Error returned from running the command: " & intRC
   WScript.Echo "Command attempted: " & strCommand 
else
   WScript.Echo "Command executed successfully."
end if

Discussion

The subst command is a useful utility for making folders on a volume appear as a drive. Let's say, for example, that you like to store files in your user profile (e.g., C:\Documents and Settings\rallen\My Documents\scripts) and need ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.