What is the proper way to test if a parameter 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 %1
is surrounded by quotes such as the case when %1
is "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 IF
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
UPDATE: on 2020-10-25, I updated the accepted answer from using brackets to using a tilde. Everyone says the tilde is better as it's more secure. I'm a little torn cause the tilde looks more complicated and is less clear as to what it's purpose is but nevertheless, I changed it.
Solution 1:
Use square brackets instead of quotation marks:
IF [%1] == [] GOTO MyLabel
Parentheses are insecure: only use square brackets.
Solution 2:
You can use:
IF "%~1" == "" GOTO MyLabel
to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it.
Solution 3:
One of the best semi solutions is to copy %1
into a variable and then use delayed expansion, as delayedExp. is always safe against any content.
set "param1=%~1"
setlocal EnableDelayedExpansion
if "!param1!"=="" ( echo it is empty )
rem ... or use the DEFINED keyword now
if defined param1 echo There is something
The advantage of this is that dealing with param1 is absolutly safe.
And the setting of param1 will work in many cases, like
test.bat hello"this is"a"test
test.bat you^&me
But it still fails with strange contents like
test.bat ^&"&
To be able to get a 100% correct answer for the existence
It detects if %1
is empty, but for some content it can't fetch the content.
This can be also be useful to distinguish between an empty %1
and one with ""
.
It uses the ability of the CALL
command to fail without aborting the batch file.
@echo off
setlocal EnableDelayedExpansion
set "arg1="
call set "arg1=%%1"
if defined arg1 goto :arg_exists
set "arg1=#"
call set "arg1=%%1"
if "!arg1!" EQU "#" (
echo arg1 exists, but can't assigned to a variable
REM Try to fetch it a second time without quotes
(call set arg1=%%1)
goto :arg_exists
)
echo arg1 is missing
exit /b
:arg_exists
echo arg1 exists, perhaps the content is '!arg1!'
If you want to be 100% bullet proof to fetch the content, you could read How to receive even the strangest command line parameters?
Solution 4:
From IF /?:
If Command Extensions are enabled IF changes as follows:
IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command
......
The DEFINED conditional works just like EXISTS except it takes an environment variable name and returns true if the environment variable is defined.