84

I turned off echo in 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.

message between those two echos.

What can be the reason of this message and why message ignores echo off?

3
  • 1
    If the path has spaces is it quoted? if not if exist "%INSTALL_PATH%" (...
    – Alex K.
    Commented Jan 11, 2012 at 17:23
  • 4
    Warnings are displayed even if you are set echo to off, @echo off just means that no commands should be echoed to the terminal. Commented Jan 11, 2012 at 17:29
  • 1
    In addition to adding quotes around the path, add a space before the (
    – dbenham
    Commented Jan 11, 2012 at 17:50

6 Answers 6

127

As Mike Nakis said, echo off only prevents the printing of commands, not results. To hide the result of a 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 Krister Andersson said, the reason you get an error is your variable is expanding with spaces:

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

Becomes:

if exist C:\My App\Installer (

Which means:

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

You see the error because you have no folder named "App". Put quotes around the path to prevent this splitting.

1
  • i've quotted %INSTALL_PATH%. that message disapeared. but i've got new error. "( was unexpected at this time." i'll ask another question. Thanks! Commented Jan 12, 2012 at 9:12
52

Save this as *.bat file and see 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
2
  • I'm not sure what you're trying to accomplish since the question has accepted answer... Commented May 27, 2016 at 20:24
  • 17
    Nothing, just another answer. Maybe it will be more clear than aaccepted answer to someone. Commented May 27, 2016 at 20:26
12

"echo off" is not ignored. "echo off" means that you do not want the commands echoed, it does not say anything about the errors produced by the commands.

The lines you showed us look okay, so the problem is probably not there. So, please show us more lines. Also, please show us the exact value of 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;
)
3
  • 1
    You can also put the quotes around the variable: IF EXIST "%INSTALL_PATH%".
    – aphoria
    Commented Jan 11, 2012 at 17:48
  • I only mention it because sometimes you need to append to variable and having the quotes as part of the value make that more difficult.
    – aphoria
    Commented Jan 11, 2012 at 18:56
  • 2
    Also, you don't want the double backslashes...one will do, C:\etc etc\test.
    – aphoria
    Commented Jan 11, 2012 at 18:57
4

For me this issue was caused by the file encoding format being wrong. I used another editor and it was saved as UTF-8-BOM so the very first line I had was @echo off but there was a hidden character in the front of it.

So I changed the encoding to plain old ANSI text, and then the issue went away.

0
-1

save document as ANSI

you can use notepad++

2
  • This may work for you. But the real reason it didn't work initially may not be this trivial. In general I advice not to ignore decades of progress in text encoding. Use UTF-8. Commented Oct 16, 2023 at 20:12
  • Also please don't assume that everyone on Earth uses Windows( Notepad++ is a Windows program). Commented Oct 16, 2023 at 20:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.