Recipe 3.13. Creating a Virtual Drive to Another Drive or FolderProblemYou want to make a folder the root of a drive or you want to use multiple drive letters for the same drive. SolutionUsing a command-line interfaceUse 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 VBScriptThere 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 DiscussionThe 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 to frequently access those files from a command line. You are starting to shown signs of getting carpal tunnel syndrome because even with tab-completion enabled, it takes a bit of typing to type out that path. You can use subst to create a drive that is mapped to that folder path and save yourself a lot of typing. There are a few caveats to be aware of when using subst:
See AlsoRecipe 3.12, MS KB 218740 (Cannot Use Subst.exe with UNC Path), and MS KB 269163 (Drives Created with the Subst Command Are Not Connected) |