开发者

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:\Windo开发者_开发技巧ws\> 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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜