windowscmdsubst

cmd - Is it possible to temporarily assign an available drive letter to a local path?


Using cmd on Windows, it is easy to assign a drive letter to a UNC path with pushd:

C:\Windows\> pushd \\server\share\path
Y:\> popd
C:\Windows\>

However I would like to be able to do the same with local paths because it will shorten the file paths and I have to use commands that do not support files having a very long path.

The idea is the following without the G: hardcoded in the script, because it could be used on another machine.

subst G: .
pushd G:\
(other commands)
popd
subst G: /d

I have tried pushd \\?\%CD% but unfortunately it does not work…

Does anybody have a magic trick for that?

Thank you


Solution

  • If your'e on windows 7 you don't have to use drive letters. You can create a symbolic link instead.

    To link to a folder use:

    cd <folder_you_want_the_link_in>
    mklink /D \MyLinkedFolder \Folder\Folder\Folder\Folder\MyLinkedFolder