【问题标题】:cmd - Is it possible to temporarily assign an available drive letter to a local path?cmd - 是否可以临时将可用驱动器号分配给本地路径?
【发布时间】:2011-09-20 08:23:39
【问题描述】:

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

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

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

下面的想法是在脚本中没有硬编码G:,因为它可以在另一台机器上使用。

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

我试过pushd \\?\%CD%,但不幸的是它不起作用……

有没有人有这方面的魔术?

谢谢

【问题讨论】:

  • 路径很长有什么问题?您是否考虑过只使用net use
  • @tenour:某些应用程序不支持它们(缓冲区太小)。 net use 需要像 subst 这样的驱动器号。
  • 对我有用的一件事是使用 8.3 路径,您不太可能遇到 260 个字符的 MAX_PATH。
  • @ewall:从 Windows 7 开始,你不能依赖 8.3 路径的可用性。
  • @Benoit 引用真实。其实disable 8.3 paths all the way back to WinNT是可以的。所以使用 8.3 路径绝对不是可靠的、万无一失的方法。另一方面,如果您不能轻松use the Unicode/"wide" Win32 API calls,它通常适用于一次性系统管理员脚本。

标签: windows cmd subst


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    这是一个我不喜欢的临时解决方案,但尝试以编程方式查找从 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
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 2013-05-08
      • 2022-09-23
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多