Windows batch file if else usage

Sorry I am new to this stuff. I'd like to run in a certain sequence the same bat file with different parameters. I wrote a very simple batch file:

@echo off
REM Note: to see all command line usage options, run bsearch_headless.bat without any arguments.
call behaviorsearch_headless.bat -p test_behaviorsearch.bsearch -o pippo
ECHO
IF EXIST pippo.finalBests.csv (call behaviorsearch_headless.bat -p test_behaviorsearch.bsearch -o topolino)
else goto :eof
:eof
ECHO Simulatione End!
PAUSE

It does not work because else is not recognized.

Many thanks for any help!

2

2 Answers

From the if documentation on the command line (via help if or available in TechNet too).

The ELSE clause must occur on the same line as the command after the IF. For example:

IF EXIST filename. ( del filename.
) ELSE ( echo filename. missing.
)

The following would NOT work because the del command needs to be terminated by a newline:

IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line as the end of the IF command:

IF EXIST filename. del filename.
ELSE echo filename. missing


So, your script would work if you replaced

IF EXIST pippo.finalBests.csv (call behaviorsearch_headless.bat -p test_behaviorsearch.bsearch -o topolino)
else goto :eof 

With

IF EXIST pippo.finalBests.csv (call behaviorsearch_headless.bat -p test_behaviorsearch.bsearch -o topolino) else goto :eof

OR

IF EXIST pippo.finalBests.csv ( call behaviorsearch_headless.bat -p test_behaviorsearch.bsearch -o topolino
) else ( goto :eof
)

Hope that helps.

ifelf.cmd:

@ECHO OFF
@IF EXIST "C:\boot.ini" (
@ECHO WoW! It may be M$Windows!
) ELSE (
@ECHO Boot.ini Lost! My precious! Stolen!
)
@ECHO .
@ECHO Wait 10 sec ...
@ping 127.0.0.1 -n 10 > NUL
5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like