cmd - Is it possible to temporarily assign an avai

2019-08-03 11:53发布

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

2条回答
太酷不给撩
2楼-- · 2019-08-03 12:11

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
查看更多
Anthone
3楼-- · 2019-08-03 12:27

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
查看更多
登录 后发表回答