Reputation: 86627
How could I modify the following example code to check if the input parameter was given when starting the batch file?
Because the check IF NOT %MYDIR%==test
fails and terminates the batch process if no paramter was provided.
SET MYDIR=%1
IF {no parameter given} OR NOT %MYDIR%==test (
ECHO dir is not "test"
)
Upvotes: 0
Views: 2798
Reputation: 130809
It is surprisingly difficult to handle all possibilities when dealing with passed parameters. But the following strategy works under most "ordinary" situations.
if "%~1" equ "" echo arg 1 was not passed
It is important that the ~
modifier is used because you have no way of knowing if the passed argument is already enclosed in quotes. If an argument like "this&that"
is passed and you don't first remove the quotes before adding your own, then you get if ""this&that"" equ ""
. The &
is no longer quoted and your command no longer parses properly.
Upvotes: 6
Reputation: 23300
Strings cannot be completely empty, a common way to work around this constraint is to enclose strings in quotes like this
... OR NOT "%MYDIR%"=="test"
or you can add something meaningless without enclosing the string (ugly!)
... OR NOT XXX%MYDIR%==XXXtest
Upvotes: 0