How to check command line parameter in ".bat" file?

233,173

Solution 1

You need to check for the parameter being blank: if "%~1"=="" goto blank

Once you've done that, then do an if/else switch on -b: if "%~1"=="-b" (goto specific) else goto unknown

Surrounding the parameters with quotes makes checking for things like blank/empty/missing parameters easier. "~" ensures double quotes are stripped if they were on the command line argument.

Solution 2

Look at http://ss64.com/nt/if.html for an answer; the command is IF [%1]==[] GOTO NO_ARGUMENT or similar.

Solution 3

The short answer - use square brackets:

if [%1]==[] goto :blank

or (when you need to handle quoted args, see the Edit below):

if [%~1]==[] goto :blank

Why? you might ask. Well, just as Jeremiah Willcock mentioned: http://ss64.com/nt/if.html - they use that! OK, but what's wrong with the quotes?

Again, short answer: they are "magical" - sometimes double (double) quotes get converted to a single (double) quote. And they need to match, for a start.

Consider this little script:

@rem argq.bat
@echo off

:loop 
if "%1"=="" goto :done
echo %1
shift
goto :loop

:done
echo Done.

Let's test it:

C:\> argq bla bla
bla
bla
Done.

Seems to work. But now, lets switch to second gear:

C:\> argq "bla bla"
bla""=="" was unexpected at this time.

Boom This didn't evaluate to true, neither did it evaluate to false. The script DIED. If you were supposed to turn off the reactor somewhere down the line, well - tough luck. You now will die like Harry Daghlian.

You may think - OK, the arguments can't contain quotes. If they do, this happens. Wrong Here's some consolation:

C:\> argq ""bla bla""
""bla
bla""
Done.

Oh yeah. Don't worry - sometimes this will work.

Let's try another script:

@rem args.bat
@echo off

:loop 
if [%1]==[] goto :done
echo %1
shift
goto :loop

:done
echo Done.

You can test yourself, that it works OK for the above cases. This is logical - quotes have nothing to do with brackets, so there's no magic here. But what about spicing the args up with brackets?

D:\>args ]bla bla[
]bla
bla[
Done.

D:\>args [bla bla]
[bla
bla]
Done.

No luck there. The brackets just can't choke cmd.exe's parser.

Let's go 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 pass just:

D:\>argq bla2"
The syntax of the command is incorrect.

The script won't run at all. Same for args.bat:

D:\>args bla2"
The syntax of the command is incorrect.

But what do I get, when the number of "-characters "matches" (i.e. - is even), in such a case:

D:\>args bla2" "bla3
bla2" "bla3
Done.

NICE - I hope you learned something about how .bat files split their command line arguments (HINT: *It's not exactly like in bash). The above argument contains a space. But the quotes are not stripped automatically.

And argq? How does it react to that? Predictably:

D:\>argq bla2" "bla3
"bla3"=="" was unexpected at this time.

So - think before you say: "Know what? Just use quotes. [Because, to me, this looks nicer]".

Edit

Recently, there were comments about this answer - well, sqare brackets "can't handle" passing quoted arguments and treating them just as if they weren't quoted.

The syntax:

if "%~1"=="" (...)

Is not some newly found virtue of the double quotes, but a display of a neat feature of stripping quotes from the argument variable, if the first and last character is a double quote.

This "technology" works just as well with square brackets:

if [%~1]==[] (...)

It was a useful thing to point this out, so I also upvote the new answer.

Finally, double quote fans, does an argument of the form "" exist in your book, or is it blank? Just askin' ;)

Solution 4

In addition to the other answers, which I subscribe, you may consider using the /I switch of the IF command.

... the /I switch, if specified, says to do case insensitive string compares.

it may be of help if you want to give case insensitive flexibility to your users to specify the parameters.

IF /I "%1"=="-b" GOTO SPECIFIC

Solution 5

You are comparing strings. If an arguments are omitted, %1 expands to a blank so the commands become IF =="-b" GOTO SPECIFIC for example (which is a syntax error). Wrap your strings in quotes (or square brackets).

REM this is ok
IF [%1]==[/?] GOTO BLANK

REM I'd recommend using quotes exclusively
IF "%1"=="-b" GOTO SPECIFIC

IF NOT "%1"=="-b" GOTO UNKNOWN
Share:
233,173

Related videos on Youtube

javauser71
Author by

javauser71

Updated on July 08, 2022

Comments

  • javauser71
    javauser71 almost 2 years

    My OS is Windows Vista. I need to have a ".bat" file where I need to check if user enters any command-line parameter or not. If does then if the parameter equals to -b then I will do something otherwise I will flag "Invalid input". If user does not enter any command-line parameter then I will do something. I have created following .bat file. It works for -b and not-equal to -b cases - but it fails when user does not pass any command-line parameter.

    I always get error:

    GOTO was unexpected at this time.
    

    Can anyone tell me what am I doing wrong here?


    ECHO OFF
    CLS
    ECHO.
    
    IF [%1]==[/?] GOTO BLANK
    
    IF %1=="-b" GOTO SPECIFIC
    
    IF NOT %1=="-b" GOTO UNKNOWN
    
    :SPECIFIC
    
    ECHO SPECIFIC
    
    GOTO DONE
    
    :BLANK
    
    ECHO No Parameter
    
    GOTO DONE
    
    :UNKNOWN
    
    ECHO Unknown Option
    
    GOTO DONE
    
    :DONE
    
    ECHO Done!
    
    • Jeremiah Willcock
      Jeremiah Willcock about 13 years
      If you add brackets (like in the GOTO BLANK line) to the other two IF statements, does that fix the problem?
  • javauser71
    javauser71 about 13 years
    This works!! Only "else" is not accepted. I get error: 'else' is not recognized as an internal or external command, operable program or batch file. So I added another IF for "not equal to -b" case. Thanks for the quick answer.
  • javauser71
    javauser71 about 13 years
    IF [%1]==[/?] is not working but of I do IF [%1]==[] or "%1"=="", then it works. Anyway now I can get going. Thanks for your quick response.
  • jeb
    jeb about 13 years
    The ELSE have to be on the same line as the IF, or it has to be directly after a bracket
  • Mark A. Fitzgerald
    Mark A. Fitzgerald over 9 years
    This does not seem to work for me if %1 contains spaces, which may be the case if it is the full path to an executable. See Jeremiah Willcock's answer for a solution that works even for parameters with spaces in their values.
  • Admin
    Admin over 8 years
    it won't be accepted if your argument is a File, in that case your should be checking only exist or not exist. This is why your arguments should be well defined, or simply use powershell or vbScript (if you're in the 80's..)
  • Tomasz Gandor
    Tomasz Gandor over 7 years
    Not only does this ALSO work! It just works, in contrast to "%1"=="". I have to elaborate on that in my own answer.
  • matt wilkie
    matt wilkie almost 7 years
    this will break when %1 is quoted, e.g. foo.bat "1st parameter" 2nd_param. See stackoverflow.com/questions/2541767/…
  • wisbucky
    wisbucky over 6 years
    This fails for run.bat "a b". "%1"=="" crashes if the arg has a space. See stackoverflow.com/a/46942471
  • wisbucky
    wisbucky over 6 years
    Using [%1]==[-b] will not match if arg is quoted. Example run.bat "-b". See stackoverflow.com/a/46942471
  • wisbucky
    wisbucky over 6 years
    Square brackets [%1]==[-b] will not match if arg is quoted like run.bat "-b". See stackoverflow.com/a/46942471
  • Skip R
    Skip R over 6 years
    Historically note: [%1]==[-b] and "%1"=="-b" were the same for win 98 and earlier MS/PC-DOS systems batch scripts. Since win 2000/NT introduced syntax if "%~1"=="-b" where double quotes have special meaning that is the way you should code scripts as it provides more robust protection. Double quotes escape the meaning of special characters (try & | and % chars in your command line). 99.9% of examples work with double quotes syntax - your example argq bla2" "bla3 is only case to justify square brackets. Embedded double quotes is a recipe for disaster - just sayin'
  • Tomasz Gandor
    Tomasz Gandor over 6 years
    This is not the end of square brackets, you see: IF [%~1]==[] - this works, and it even handles "-b". You just need to be able to think inside the box, erm, square [brackets].
  • Tomasz Gandor
    Tomasz Gandor over 6 years
    @SkipR - that's why in Windows' filesystems, you can't have these special characters in names. I don't have a mathematical proof, but I think that simply NOT everything can be quoted (in the sense of - made verbatim via some escape sequence etc.) in the cmd shell. In other systems you can sometimes quote everything, NUL character included. (stackoverflow.com/questions/2730732/…) - this question just shows, you need to use some external program.
  • David Pierson
    David Pierson over 2 years
    This works for me even when the argument is quoted. e.g. foo.bat "1st parameter" works fine in my testing. The square brackets seem better than IF "%1"==""