cmd - 是否可以临时为本地路径分配可用的驱动器号?

时间:2011-09-20 08:23:39

标签: windows cmd subst

在Windows上使用cmd,使用pushd很容易为UNC路径分配驱动器号:

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

但是我希望能够对本地路径做同样的事情,因为它会缩短文件路径,我必须使用不支持具有很长路径的文件的命令。

如果脚本中没有G:硬编码,则会出现以下情况,因为它可以在另一台计算机上使用。

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

我已经尝试pushd \\?\%CD%,但不幸的是它不起作用......

有人有这个神奇的伎俩吗?

谢谢

2 个答案:

答案 0 :(得分:5)

如果您在Windows 7上,则不必使用驱动器号。您可以改为创建符号链接。

要链接到文件夹,请使用:

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

答案 1 :(得分:0)

这是一个我不喜欢的临时解决方案,但尝试以编程方式查找从Z:开始的第一个可用驱动器号,如pushd所做的那样。我想它很容易失败。

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