【问题标题】:Echo off but messages are displayed回显关闭,但显示消息
【发布时间】:2012-02-08 01:36:27
【问题描述】:

我在 bat 文件中关闭了 echo。

@echo off

然后我做这样的事情

...
echo %INSTALL_PATH%
if exist %INSTALL_PATH%(
echo 222
...
)

我得到:

系统找不到指定的路径。

这两个回声之间的消息。

此消息可能是什么原因以及为什么消息忽略回显?

【问题讨论】:

  • 如果路径有空格,它是否被引用?如果不是if exist "%INSTALL_PATH%" (...
  • 即使您将 echo 设置为 off,也会显示警告,@echo off 仅表示不应将任何命令回显到终端。
  • 除了在路径两边加上引号,在 (

标签: batch-file cmd echo


【解决方案1】:

正如Mike Nakis 所说,echo off 只阻止打印命令,而不是结果。要隐藏命令的结果,请将>nul 添加到行尾,并隐藏错误添加2>nul。例如:

Del /Q *.tmp >nul 2>nul

就像Krister Andersson 所说,你得到错误的原因是你的变量用空格扩展:

set INSTALL_PATH=C:\My App\Installer
if exist %INSTALL_PATH% (

变成:

if exist C:\My App\Installer (

这意味着:

如果“C:\My”存在,则使用“(”作为命令行参数运行“App\Installer”。

您看到错误是因为您没有名为“App”的文件夹。在路径周围加上引号以防止这种分裂。

【讨论】:

  • 我引用了 %INSTALL_PATH%。那条消息消失了。但我有新的错误。 “(此时出乎意料。”我会问另一个问题。谢谢!
【解决方案2】:

将此保存为 *.bat 文件并查看差异

:: print echo command and its output
echo 1

:: does not print echo command just its output
@echo 2

:: print dir command but not its output
dir > null

:: does not print dir command nor its output
@dir c:\ > null

:: does not print echo (and all other commands) but print its output
@echo off
echo 3

@echo on
REM this comment will appear in console if 'echo off' was not set

@set /p pressedKey=Press any key to exit

【讨论】:

  • 我不确定您要完成什么,因为该问题已接受答案...
  • 没什么,只是另一个答案。也许它会比接受某人的答案更清楚。
【解决方案3】:

“回声关闭”不会被忽略。 “echo off”表示您不希望命令回显,它不会说明命令产生的错误。

您向我们展示的线条看起来不错,所以问题可能不存在。所以,请给我们看更多的台词。另外,请告诉我们 INSTALL_PATH 的确切值。

【讨论】:

    【解决方案4】:
    @echo off
    // quote the path or else it won't work if there are spaces in the path
    SET INSTALL_PATH="c:\\etc etc\\test";
    if exist %INSTALL_PATH% (
       //
       echo 222;
    )
    

    【讨论】:

    • 你也可以在变量两边加上引号:IF EXIST "%INSTALL_PATH%".
    • 我只提到它是因为有时您需要附加到变量并且将引号作为值的一部分使这更加困难。
    • 另外,你不想要双反斜杠......一个可以的,C:\etc etc\test
    【解决方案5】:

    对我来说,这个问题是由文件编码格式错误引起的。 我使用了另一个编辑器,它被保存为UTF-8-BOM,所以我的第一行是@echo off,但它前面有一个隐藏字符。

    所以我将编码更改为普通的旧 ANSI 文本,然后问题就消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2012-08-17
      • 2012-12-25
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多