sutterhome1971
sutterhome1971

Reputation: 400

pause in batch file not working

I have a batch file that tests to see if a file exists. After that, it has to pause.

But pause is not working for some reason.
Any help please. thanks much

  if exist C:\autoexec.bat 
  ECHO Folder C:\autoexec.bat exists
  PAUSE

Pause works at command prompt.

The file exists for sure. But I can't see Echo as well as Pause The screen just disappears once I run the .bat file

Upvotes: 0

Views: 4360

Answers (3)

João
João

Reputation: 2323

Do you want to write:

if exist C:\autoexec.bat ECHO Folder C:\autoexec.bat exists
PAUSE

Note that shell after the if command is expecting a command to execute. If there is no command after de condition it should abort batch with: "The syntax of the command is incorrect."

Edit. Mr.Helpy Got it first.

Note if you want multiple lines use a block

if exist C:\autoexec.bat (
ECHO Folder C:\autoexec.bat exists
REM other commands...
)
PAUSE

Upvotes: 3

Stephan
Stephan

Reputation: 56180

Basic troubleshooting: run from a cmdwindow, not per doubleclick.

Then you see, that

 if exist C:\autoexec.bat 

give you a syntax error, that breaks the execution of your batchfile.

if expects a command to execute (at the same line), but there isn't one. So the echo line is never reached.

Upvotes: 2

Mr.Helpy
Mr.Helpy

Reputation: 157

The answer is in the command. The valid syntax for if command is

IF [NOT] EXIST filename command 
OR
IF [NOT] EXIST filename (command) ELSE (command)

The word command at the last is very important. What I did was this

@Echo off
if exist C:\autoexec.bat goto hi
if not exist C:\autoexec.bat goto bye
:hi
ECHO Folder C:\autoexec.bat exists
PAUSE
:bye
Echo Folder C:\autoexec.bat does not exists
pause

And it worked like a charm

Regards,

See http://ss64.com/nt/if.html

Upvotes: 2

Related Questions