Disable echo but messages are displayed

I disabled the echo in the bat file.

@echo off 

then i do something like this

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

and I get:

The system cannot find the path specified.

between these two echoes.

What is the reason for this message and why does the message ignore the echo?

+62
cmd echo batch-file
Jan 11 '12 at 17:18
source share
6 answers

As Mike Nakis said, echo off only prevents printing commands, not results. To hide the result of the command, add >nul to the end of the line and to hide errors add 2>nul . For example:

 Del /Q *.tmp >nul 2>nul 

Like Christer Andersson , the reason you get the error message is because your variable expands with spaces:

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

becomes:

 if exist C:\My App\Installer ( 

It means:

If "C: \ My" exists, run "App \ Installer" with "(" as a command line argument.

You see an error because you do not have a folder named "Application". Place quotation marks around the path to prevent this splitting.

+103
Jan 11 '12 at 22:13
source share

Save it as a * .bat file and see the differences

 :: 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 
+33
May 26 '16 at 13:23
source share

"echo off" is not ignored. "echo off" means that you do not want the commands to be echoed; it does not say anything about errors generated by the commands.

The lines that you showed us look good, so there is probably no problem. So please show us more lines. Also, please show us the exact value of INSTALL_PATH.

+10
Jan 11 '12 at 17:24
source share
 @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; ) 
+4
Jan 11 '12 at 17:38
source share

For me, this problem was caused by the wrong file encoding format. I used a different editor, and it was saved as UTF-8-BOM so the very first line I had was @echo off but @echo off it had a hidden character.

So I changed the encoding to plain old ANSI text, and then the problem disappeared.

+3
Feb 23 '18 at 15:18
source share

Yes, it worked for me too - although @echo off was not on the first line. In my case, changing the file encoding format from UTF-8 to ANSI solved the problem.

0
Dec 12 '18 at 22:04
source share



All Articles