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

风格不统一 提交于 2019-12-11 07:44:52

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/7482137/cmd-is-it-possible-to-temporarily-assign-an-available-drive-letter-to-a-local

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!