Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If elseif else in windows batch file

I have a bat script for which I need to provide a parameter. If that parameter equals, "my-test1", it will execute a script. If that parameter equals "my-test2", it will execute another one. If the parameter does not exist in any of the if/elseif, return code 1.

How could I achieve this? Please find below what I’ve tried so far.

   if "%~1" == "my-test1" (
            python.exe mypath\my_test1.py
            )
    elseif "%~1" == "my-test2" (
            python.exe mypath\my_test2.py
            )
    else
        EXIT WITH AN EXECUTION CODE 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

else and if are separate keywords. Put a space between them.
EXIT WITH AN EXECUTION CODE 1 should be exit /b 1

AND else must be on the same physical line as the ) closing the true conditional processing

AND else must either be followed by some command(s) or ( and commands on the next line(s)

ie.

 if "%~1" == "my-test1" (
            python.exe mypath\my_test1.py
            ) else if "%~1" == "my-test2" (
            python.exe mypath\my_test2.py
            ) else EXIT /b 1

Use if /i to do a case-insensitive match

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading