0

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

6
  • Pause "works" just fine. Not enough information here to tell what you are doing wrong. Commented Mar 11, 2016 at 16:09
  • Is your problem that the pause is always executed and you only want it to pause when the file exists? Commented Mar 11, 2016 at 16:12
  • "not working"? any output? Does it work "standalone" (just pause at the command prompt)? Commented Mar 11, 2016 at 16:25
  • Welcome to SO. Please take two-minute tour and follow How to Ask. "not working" seems to be insufficient… Commented Mar 11, 2016 at 16:53
  • 1
    @bnath001 No, If there is syntax error it aborts the script and forces it to end Commented Mar 12, 2016 at 18:38

3 Answers 3

3

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 
Sign up to request clarification or add additional context in comments.

3 Comments

putting in block helped.
what if we have to check multiple files?
@bnath001 - Use multiple ifs
2

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

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.