2012-01-11 110 views
53

我关闭了bat文件中的回声。回声消息但显示消息

@echo off 

然后我做这样的事情

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

,我也得到:

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

这两个回声之间的消息。

此消息的原因是什么以及消息忽略回显的原因是什么?

+0

如果路径中有空格,是否有引号?如果不存在“%INSTALL_PATH%”(...' – 2012-01-11 17:23:25

+2

即使将回显设置为off,“@echo off”也会显示警告,这意味着没有命令应回显给终端。 – Cyclonecode 2012-01-11 17:29:23

+0

除了在路径中添加引号之外,还可以在( – dbenham 2012-01-11 17:50:52

回答

95

正如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 \ Install呃“与”(“作为命令行参数。

由于没有名为“App”的文件夹,您会看到该错误。在路径上加上引号以防止这种分裂。

+0

我引用了%INSTALL_PATH%。该消息消失了,但我得到了新的错误。“(此时意外。”我会问另一个问题。谢谢! – 2012-01-12 09:12:24

10

“echo off”不被忽略。 “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; 
) 
+1

之前添加一个空格。您还可以在变量周围加上引号:'IF EXIST“%INSTALL_PATH%”'。 – aphoria 2012-01-11 17:48:09

+0

@aphoria - Jupp, – Cyclonecode 2012-01-11 17:54:39

+0

我只提到它,因为有时你需要追加到变量中,并将引号作为值的一部分使得更加困难 – aphoria 2012-01-11 18:56:26

19

保存为* .bat文件,看看差别

:: print echo command and it's output 
echo 1 

:: does not print echo command just it's output 
@echo 2 

:: print dir command but not it's output 
dir > null 

:: does not print dir command nor it's output 
@dir c:\ > null 

:: does not print echo (and all other commands) but print it's 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 
+0

I 'm不知道你试图完成什么,因为这个问题已经接受了答案... – 2016-05-27 20:24:29

+5

没什么,只是另一个答案。也许它会比接受答案更清楚对某人。 – 2016-05-27 20:26:23

+0

我希望它会:) – 2016-05-28 11:45:01

0

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

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