Recipe 7.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 get carpal tunnel syndrome because even with tab-completion enabled, it takes a bit of wrist work 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 create a persistent virtual drive you need to use a logon script.


Shadow copies are not created

On Windows XP, 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 7.12 for mapping a network drive; MS KB 218740, "Cannot Use Subst.exe with UNC Path," and MS KB 269163, "Drives Created with the Subst Command Are Not Connected"



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

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