Mounting folders as drives (without SUBST)
04 Jul 2021When I got my first job as a developer in '97 my coworker introduced me to using a drive as the root for all the source code. This is awesome because it means it's super fast to get to, no matter where you are: some Windows file dialog, Windows Explorer, or the terminal. Your source code is always only three letters away. Of course, this technique isn't limited to source code. It can apply to anything you're working on: documents, photos, or videos.
For the longest time I've used SUBST
to achieve this, but in this post I'm
going to show a much better alternative.
What's wrong with SUBST?
Using SUBST
is pretty easy. You only need a batch file like this:
@echo off
subst P: D:\Projects
subst T: D:\Trash
In this case, I'm mounting D:\Project
as my P:
drive and D:\Trash
as my
T:
drive.
The issue with SUBST
is that it's only visible to the current user. For most
cases that seems appropriate but even if you only use a single user, those
drives aren't visible inside an elevated context, which can be extremely
annoying. Also, there is a minor issue that the batch file needs to be
registered as a startup script which sometimes means you have to wait after a
reboot before you can access the drives. Not a biggie, but still.
The solution
I recently did some digging and found this nifty solution on Super User. Basically, it registers a virtual drive for the entire machine. You can even run services off of it!
The only caveat is that you need to prefix the path with \??\
. In case you use
a .reg
file (which you should so you can recreate this when you need to set up
your machine again), you also need to escape the backslashes by doubling them.
For my case this looks as follows:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices]
"P:"="\\??\\D:\\Projects"
"T:"="\\??\\D:\\Trash"
Just put the content in a file with the extension of .reg
, run it, and restart
your machine. Done!
Name and Icon
While you can't rename these virtual drives by using properties, you can assign default labels and icons in Windows Explorer.
For this to work, the drive name of the underlying drive has to be empty. For
example, if you mount C:\work
as D:\
, then the name of the C drive needs to
be empty. In order to see a name in Windows Explorer you need to assign the
DefaultLabel
under the DriveIcons
folder for the respective drive.
In my case all the folders are on the D:
drive, so I remove the drive name for
it and set its default label to Data
:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\D]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\D\DefaultLabel]
@="Data"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\P]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\P\DefaultLabel]
@="Projects"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\P\DefaultIcon]
@="D:\\Tools\\Icons\\projects.ico"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\T]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\T\DefaultLabel]
@="Temporary Projects"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\T\DefaultIcon]
@="D:\\Tools\\Icons\\temp_projects.ico"
How I use it
Some people use C:\src
as the root for their source code. Sure, that works
too. But I generally don't want to store anything on my C:
drive that I care
about. For me, C:
is only for the OS and apps. Everything else goes on D:
,
which is usually a physical drive or at least a partition. This makes it very
easy to reset the machine too. No need to worry about accidentally nuking any
documents or projects you care about.
Of course, D:\src
would equally work, but I like to organize my D:
drive a
bit more, but I don't want to sacrifice how easy it is to get to.
Also, using virtual drives allows you to standardize the drives between machines. For example, I sometimes don't have a secondary drive or I have multiple drives so on different machines my source code might actually lie on different drives. With a virtual drive letter that is in the middle of the alphabet you can likely ensure that you can use the same letter across all machines.
For the most part I use P:
and T:
drives:
-
P:
represents the home of projects, so each top level folder is some git repository. -
T:
represents all the temporary stuff I'm doing. You know, whereConsoleApplication32
lives.
At some point I also had M:
where I stored media files (e.g. icons) and V:
where I put my videos for editing.
Caveats
There are a few restrictions for virtual drives:
- You can't name them. They inherit the drive name from the directory's drive (see above for the workaround in Windows explorer).
- Can't use the recycle bin. You can't use recycle bin functionality on virtual drives, that is all deletions are permanent.
Summary
Virtual drives are awesome. SUBST
can be awkward but when you register them in
the Windows Registry, they just work great.