1.

Solve : How do I check if the parameter %1 exist in a batch file (IF statement)??

Answer»

I tried the following batch file:

ECHO OFF
:start
Echo - %1
shift
IF %1=="" exit
pause
goto start

and run it as shift.bat 1 2 3

After the 3rd parameter, I want the program to exit. However, the IF statement
IF %1=="" exit
is incorrect. I also tried
IF %1=="" echo - Missing
but this was also incorrect...

What is the correct syntax to evaluate if %1 is missing?both sides need to evaluate to something; in your code, once you get to the third parameter, the if BECOMES
Code: [Select]IF =="" EXIT
which is invalid syntax.

use:
Code: [Select]IF "%1"=="" EXIT
Of course. Need to "define" the %1 as a string.
Thanks! Quote from: viking2 on February 28, 2010, 02:25:28 PM

Of course. Need to "define" the %1 as a string.
Thanks!
well, not really... you could just as easily do

Code: [Select]IF !%1==! EXIT

It's just when it expands so there is nothing on one side, it's not a valid syntax. Quote from: viking2 on February 28, 2010, 01:27:58 PM
I tried the following batch file:
What is the correct syntax to evaluate if %1 is missing?

Quotes around "%1"  worked

C:\batch>type  shift.bat

Code: [Select]ECHO OFF
:start
Echo - %1
shift
IF "%1"=="" exit /B
pause
goto start

Output:

C:\batch>shift.bat   1 2 3 4
- 1
Press any key to continue . . .
- 2
Press any key to continue . . .
- 3
Press any key to continue . . .
- 4

C:\batch>

p.s.:  use exit /b  to exit the batch and not exit the command PROMPT PAGE.


Discussion

No Comment Found