windows 意外“系统找不到指定的驱动器。” 在批处理文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19843849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:03:48  来源:igfitidea点击:

Unexpected "The system cannot find the drive specified." in batch file

windowsbatch-file

提问by Matthew Strawbridge

I've got a problem with a batch file (Windows 7 x64). I've managed to reduce it to the following (there are two empty comment lines; it doesn't seem to matter whether there's an actual comment):

我的批处理文件(Windows 7 x64)有问题。我设法将其缩减为以下内容(有两个空注释行;是否有实际注释似乎并不重要):

@echo off

if /i [%1]==[] (
    echo A
    ::
    ::
    echo B
)

Putting this in a batch file and then running it with no parameters produces the following output:

把它放在一个批处理文件中,然后不带参数运行它会产生以下输出:

A
The system cannot find the drive specified.
B

Deleting one of the ::lines, or deleting the surrounding if, fixesthings so you get the expected output:

删除其中::一行,或删除周围的if修复问题,以便您获得预期的输出:

A
B

What's going on here? Why is it looking for a drive?

这里发生了什么?为什么要寻找驱动器?

Edit

编辑

Thanks for the responses for far. So my question really boils down to this:

感谢您的答复。所以我的问题实际上归结为:

What are the rules for determining whether ::means "start of comment" or "drive letter colon"?

确定是::“评论开始”还是“驱动器号冒号”的规则是什么?

Perhaps it's easier to just switch back to REMfor adding comments so there's no scope for ambiguity, but I do find the ::style easier to read.

也许切换回REM添加评论更容易,因此没有歧义的余地,但我确实发现这种::风格更容易阅读。

回答by jeb

It's an obscoure effect in batch code blocks.

这是批处理代码块中的一种晦涩的效果。

In a code block a label in one line requires always a secondary line with quite different syntax rules (for a secondaryline).
SO: goto command not working

在代码块中,一行中的标签总是需要一个具有完全不同语法规则的辅助行(对于辅助行)。
SO:goto 命令不起作用

The secondary line must be a command or simple label, but not a parenthesis nor a double colon nor an internal command appended with a parenthesis.

第二行必须是命令或简单标签,但不能是括号、双冒号或附加括号的内部命令。

MC ND explained that the :is used here like a normal drive letter, that's really true!
But only in the secondary line and even you can suppress the error by a subst :: C:\, the line is handled like a label (You can't start anything).

MC ND 解释说,:这里使用的就像一个普通的驱动器号,这是真的!
但只有在辅助行中,甚至您可以通过 a 抑制错误subst :: C:\,该行就像标签一样处理(您无法启动任何东西)。

subst :: C:\
(
:label
::\windows\system32\calc.exe
)

And the secondary line accepts &even when the line is a label.

&即使该行是标签,辅助行也接受。

Failing samples

不合格样品

(
:Label at the end fails
)

(
:label
  ( 
    echo new Block fails
  )
echo ...
)

(
:label
:: Double colon fails
)

(
:label
echo( fails also, as now a file named "echo(" is searched for execution
)

And now working ones

现在工作的

(
::Works
:with a "normal" label in the second line
)

(
:But this label! & echo This will not echo'd
:but & echo You can see this !
)

(
:label
echo or a simple command works too
)

Sometimes ::is used as a comment style, this can cause problems inside blocks, if you don't care about the secondary line.

有时::用作注释样式,如果您不关心辅助行,这可能会导致块内出现问题。

Edit: To answer your second question

编辑:回答你的第二个问题

What are the rules for determining whether :: means "start of comment" or "drive letter colon"?

确定 :: 是“评论开始”还是“驱动器号冒号”的规则是什么?

The first ::is always a comment or label, in the next line it's always a drive letter colon.

第一个::总是一个注释或标签,在下一行它总是一个drive letter colon.

回答by MC ND

::is a valid drive. Just as c:or d:but the letter is :

::是一个有效的驱动器。就像c:d:但是字母是:

So, your lines are trying to change from the current drive to drive ::

因此,您的线路正试图从当前驱动器更改为驱动器 ::

Example: try subst :: c:\and then dir ::\

示例:尝试subst :: c:\然后dir ::\