UGTS Document #41 - Last Modified: 8/29/2015 3:23 PM
Path Standardization with the SUBST Command

When synchronizing files between several machines, and those files contain shortcuts and links, you often would like a way to point to the local copy of the file in a way which is standard across many machines, though the file may not be stored in the same absolute directory on every machine.

The simplest solution is to use relative paths, but this is only an option where you can guarantee that the relative structure between the source and the destination won't change.  If you move the source or destination, you must guarantee that they will move together as a unit and the relative path between the two will not change.

If this is not the case, you still have a few options.  Shortcut (LNK files) can use environment variables in the path and still work.  For example, if you define a machine environment variable called LOCALSHARE, and point it to C:\fileshare, then you can use %LOCALSHARE%\run\App\MyApp.exe to point to C:\fileshare\run\App\MyApp.exe.  This has the advantage that it is defined at the machine level and will work even before a user logs in.  However, if is only usable in a limited number of places - shortcut files, batch files, and at the command prompt.  It will not work for hyperlinks in Word or Excel files or a number of other places.

Another solution which sometimes works is to use UNC paths, and to share a folder on each machine in the appropriate place.  You can then define machine alias name, so long as the security feature 'strict name checking' has been turned off in the registry.  You can also use \\localhost to refer to the current machine.  However, this has the problem that even if the UNC path refers to the machine you are on, the path won't work if the machine is disconnected from the network (which can happen if the machine is a laptop with wireless disconnected).  Therefore, UNC paths are only useful in places where you only need to use them while connected to the network, such as for synchronization between machines.

Another solution which is fairly robust is to use virtual drives.  The DOS 'SUBST' command can map a drive letter to a folder, which is usable by nearly all programs.  However, drive letters assigned in this way do not persist across system restarts.  To define a persistent mapping, create a registry value:

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices  value = X:

In the example, replace X: with the drive letter to be mapped, and set the value to:

\DosDevices\Y:\My\Path

Where Y:\My\Path is to be replaced with the absolute path that the drive letter is to point to.  Note that after doing this, the drive letter won't be available until you restart the computer (just logging out and logging back in is not enough).

Paths using SUBST drive letters have a fair share of limitations that you need to be aware of:
  • Drive letters mapped in this way will not be visible to system services.  Therefore, don't try to have SQL Server load a database or log file from a SUBST-defined drive letter.  Similarly, any server process which tries to use a SUBST-defined drive letter may have trouble
  • The Previous Versions feature of Windows 2003 and higher knows nothing about SUBST paths.
  • Some programs silently change a SUBST drive letter to the physical drive letter which it points to, but without changing the path part.  If the SUBST mapping maps a subdirectory with a drive letter, this program effectively can't use paths with the drive letter without getting broken paths.  Macro references in Excel (even up to Excel 2010) are notable for this deficiency.  Practically, this means that you can't reference XLA / XLAM files on a SUBST drive, if that SUBST drive points to a subdirectory on another drive.
  • Autorun.inf files on a SUBST drive are ignored.
  • In Windows XP, SUBST drives are unknown to the system process that assigns drive letters to removable media.  So, if you plugin a USB drive and the system assigns that USB drive the letter that the SUBST drive is using, suddenly the SUBST drive will be unmounted and overshadowed by the USB drive.
If you're willing to deal with these limitations, a subst-defined drive path can be used just about everywhere else to present a common filesystem structure across a variety of machines.