blak3r blak3r - 4 months ago 6x
Bash Question

What is the proper way to test if variable is empty in a batch file?

I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever

is surrounded by quotes such as the case when
"c:\some path with spaces"

IF NOT %1 GOTO MyLabel // This is invalid syntax
IF "%1" == "" GOTO MyLabel // Works unless %1 has double quotes which fatally kills bat execution
IF %1 == GOTO MyLabel // Gives an unexpected GOTO error.

According to this site, these are the supported
syntax types. So, I don't see a way to do it.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command


Use brackets instead of quotes:

IF [%1] == [] GOTO MyLabel

Note: The original answer was:
IF (%1) == () GOTO MyLabel
@jeb pointed out this was insecure, and @Synetech suggested an alternative so I revised the answer.