回显但显示消息

Ale*_*ets 69 cmd batch-file echo

我在bat文件中关闭了echo.

@echo off
Run Code Online (Sandbox Code Playgroud)

然后我做这样的事情

...
echo %INSTALL_PATH%
if exist %INSTALL_PATH%(
echo 222
...
)
Run Code Online (Sandbox Code Playgroud)

我得到:

该系统找不到指定的路径.

这两个回声之间的信息.

这条消息可能是什么原因以及为什么消息忽略了回声?

Han*_*ood 112

正如迈克纳基斯所说,echo off只能阻止打印命令,而不是结果.要隐藏命令的结果,请添加>nul到行尾,并隐藏错误添加2>nul.例如:

Del /Q *.tmp >nul 2>nul
Run Code Online (Sandbox Code Playgroud)

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

set INSTALL_PATH=C:\My App\Installer
if exist %INSTALL_PATH% (
Run Code Online (Sandbox Code Playgroud)

变为:

if exist C:\My App\Installer (
Run Code Online (Sandbox Code Playgroud)

意思是:

如果存在"C:\ My",请使用"("作为命令行参数运行"App\Installer".

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


Wak*_*nka 37

将其另存为*.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
Run Code Online (Sandbox Code Playgroud)

  • 没什么,只是另一个答案.也许它比某人接受的回答更清楚. (10认同)

Mik*_*kis 10

"回声"不被忽视."echo off"表示您不希望命令回显,它没有说明命令产生的错误.

你向我们展示的线条看起来还不错,所以问题可能不存在.所以,请告诉我们更多行.另外,请告诉我们INSTALL_PATH的确切值.