Recipe3.13.Creating a Virtual Drive to Another Drive or Folder


Recipe 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 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:

  • The drives are removed after reboot. Perhaps the biggest drawback to virtual drives is that they are removed when a machine restarts. That means to have a persistent virtual drive you need to use a logon script to create it.

  • Shadow copies are not created. On Windows Server 2003, shadow copies are created for all local volumes, but this doesn't apply to virtual drives created with subst. Since the virtual drive corresponds to a logical volume, a shadow copy is already created for its contents.

  • The drives cannot be used to set quotas. Again, due to the fact the contents of a virtual drive are already part of a volume, which may already have quotas enabled, you cannot configure quotas.

  • Deleting the virtual drive deletes only the mapping, not the data. If you delete a virtual drive using the /d option, only the drive mapping is deleted, not the underlying contents of the drive.

See Also

Recipe 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)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net