Short answer - use square brackets:
if [%1]==[] goto :blank
or (when you need to process quoted arguments, see the "Edit" section below):
if [%~1]==[] goto :blank
Why? you can ask. Well, as Jeremiah Willcock said: http://ss64.com/nt/if.html - they use it! OK, but what's wrong with the quotes?
Again, a short answer: they are "magic" - sometimes double (double) quotes are converted to a single (double) quote. And they must match, for starters.
Consider this little script:
@rem argq.bat @echo off :loop if "%1"=="" goto :done echo %1 shift goto :loop :done echo Done.
Test it:
C:\> argq bla bla bla bla Done.
It seems to need work. But now switch to second gear:
C:\> argq "bla bla" bla""=="" was unexpected at this time.
Boom . This was not evaluated as true, and false was not evaluated. script WAS DIED. If you were to shut down the reactor somewhere along the line, well - hard luck. Now you will die like Harry Daglyan.
You might think - OK, arguments cannot contain quotation marks. If they do, it will happen. Wrong Here are some consolations:
C:\> argq ""bla bla"" ""bla bla"" Done.
Oh yeah. Do not worry - sometimes it will work.
Try another script:
@rem args.bat @echo off :loop if [%1]==[] goto :done echo %1 shift goto :loop :done echo Done.
You can check yourself that it works fine for the above cases. This is logical - quotes have nothing to do with brackets, so there is no magic here. But what about hiding arguments with parentheses?
D:\>args ]bla bla[ ]bla bla[ Done. D:\>args [bla bla] [bla bla] Done.
Bad luck. The brackets simply cannot strangle the cmd.exe parser.
Let's get back to the evil quotes for a moment. The problem was there when the argument ended with a quote:
D:\>argq "bla1 bla2" bla2""=="" was unexpected at this time.
What if I just go through:
D:\>argq bla2" The syntax of the command is incorrect.
the script will not run at all. Same for args.bat :
D:\>args bla2" The syntax of the command is incorrect.
But what will I get when the number of " -characters" matches "(i.e., even), in this case:
D:\>args bla2" "bla3 bla2" "bla3 Done.
NICE . I hope you learned something about how .bat files break their command line arguments (HINT: * This is not quite the same as in bash). The above argument contains a space. But quotes are not deleted automatically.
And argq? How does he react to this? As expected:
D:\>argq bla2" "bla3 "bla3"=="" was unexpected at this time.
So think before you say, “Know what? Just use quotes. [Because it looks better to me.”
Edit
There have recently been comments about this answer - well, the square brackets “cannot handle” passing the quoted arguments and processing them the same way as if they were not quoted.
Syntax:
if "%~1"=="" (...)
Not some newly discovered virtue of double quotes, but a display of the neat function of removing quotes from a variable argument, if the first and last characters are double quotes.
This “technology” also works with square brackets:
if [%~1]==[] (...)
It was helpful to point this out, so I am also putting forward a new answer.
Finally, fans of double quotation marks, is there an argument for the form "" in your book, or is it empty? Just ask;)