1

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

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • What's wrong with really long paths? Have you considered just using `net use`? – tenfour Sep 20 '11 at 08:27
  • @tenour: some applications do not support them (buffers too small). `net use` requires a drive letter like subst. – Benoit Sep 20 '11 at 08:46
  • One thing that has worked for me is using 8.3 paths where you're much less likely to hit the 260-character MAX_PATH. – ewall Oct 07 '11 at 21:26
  • @ewall: starting from Windows 7, you can not rely on the availability of 8.3 paths. – Benoit Oct 08 '11 at 05:56
  • @Benoit Quote true. In fact, it's possible to [disable 8.3 paths all the way back to WinNT](http://support.microsoft.com/kb/121007). So using the 8.3 paths is definitely *not* a reliable, fool-proof method. On the other hand, it often works for one-off sysadmin scripts if you can't easily [use the Unicode/"wide" Win32 API calls](http://blogs.msdn.com/b/bclteam/archive/2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton.aspx). – ewall Oct 10 '11 at 13:58

2 Answers2

5

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
Asken
  • 7,679
  • 10
  • 45
  • 77
1

This is a temporary solution that I dislike but tries to find programatically the first available drive letter starting from Z: as pushd does. I suppose that it can fail easily.

call:find_first_available_drive
subst %drive% .
pushd %drive%\
(other commands)
popd
subst %drive% /d

:find_first_available_drive
@pushd Z: 2>NUL && popd || (set drive=Z:& goto:eof)
@pushd Y: 2>NUL && popd || (set drive=Y:& goto:eof)
@pushd X: 2>NUL && popd || (set drive=X:& goto:eof)
@pushd W: 2>NUL && popd || (set drive=W:& goto:eof)
@pushd V: 2>NUL && popd || (set drive=V:& goto:eof)
@pushd U: 2>NUL && popd || (set drive=U:& goto:eof)
@pushd T: 2>NUL && popd || (set drive=T:& goto:eof)
@pushd S: 2>NUL && popd || (set drive=S:& goto:eof)
@pushd R: 2>NUL && popd || (set drive=R:& goto:eof)
@pushd Q: 2>NUL && popd || (set drive=Q:& goto:eof)
@pushd P: 2>NUL && popd || (set drive=P:& goto:eof)
@pushd O: 2>NUL && popd || (set drive=O:& goto:eof)
@pushd N: 2>NUL && popd || (set drive=N:& goto:eof)
@pushd M: 2>NUL && popd || (set drive=M:& goto:eof)
@pushd L: 2>NUL && popd || (set drive=L:& goto:eof)
@pushd K: 2>NUL && popd || (set drive=K:& goto:eof)
@pushd J: 2>NUL && popd || (set drive=J:& goto:eof)
@pushd I: 2>NUL && popd || (set drive=I:& goto:eof)
@pushd H: 2>NUL && popd || (set drive=H:& goto:eof)
@pushd G: 2>NUL && popd || (set drive=G:& goto:eof)
@pushd F: 2>NUL && popd || (set drive=F:& goto:eof)
@pushd E: 2>NUL && popd || (set drive=E:& goto:eof)
@pushd D: 2>NUL && popd || (set drive=D:& goto:eof)
@pushd C: 2>NUL && popd || (set drive=C:& goto:eof)
@pushd B: 2>NUL && popd || (set drive=B:& goto:eof)
@pushd A: 2>NUL && popd || (set drive=A:& goto:eof)
@set drive=&goto:eof
Benoit
  • 76,634
  • 23
  • 210
  • 236