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

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

I always get the error:

 GOTO was unexpected at this time. 

Can someone tell me what I'm 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! 
+68
windows file cmd batch-file
Feb 10 '11 at 3:52
source share
6 answers

You need to check that the parameter is empty: if "%~1"=="" goto blank

Once you do this, run the if / else command on -b: if "%~1"=="-b" (goto specific) else goto unknown

Surrounding parameters with quotes makes it easy to check things like empty / empty / missing parameters. "~" ensures that double quotes will be separated if they were in a command line argument.

+85
Feb 10 '11 at 4:01
source share

See the http://ss64.com/nt/if.html answer; IF [%1]==[] GOTO NO_ARGUMENT command IF [%1]==[] GOTO NO_ARGUMENT or similar.

+22
Feb 10 '11 at 4:02
source share

In addition to the other answers that I sign, you can use the /I switch of the IF command.

... the / I switch, if specified, says that a case-insensitive string is compared.

this can help if you want to provide the user with insensitive flexibility to specify options.

 IF /I "%1"=="-b" GOTO SPECIFIC 
+6
Feb 10 2018-11-11T00:
source share

You are comparing strings. If arguments are omitted, %1 expands to empty, so the commands become IF =="-b" GOTO SPECIFIC for example (which is a syntax error). Wrap your lines in quotation marks (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 
+4
Feb 10 '11 at 4:02
source share

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;)

+4
Nov 09 '16 at 11:36
source share

Actually, all the other answers have flaws. The most reliable way:

 IF "%~1"=="-b" (GOTO SPECIFIC) ELSE (GOTO UNKNOWN) 

Detailed explanation:

Using "%1"=="-b" will fail to pass the argument with spaces and quotation marks. This is the least reliable method.

 IF "%1"=="-b" (GOTO SPECIFIC) ELSE (GOTO UNKNOWN) C:\> run.bat "ab" b""=="-b" was unexpected at this time. 

Using [%1]==[-b] better because it will not crash with spaces and quotation marks, but it will not match if the argument is surrounded by quotation marks.

 IF [%1]==[-b] (GOTO SPECIFIC) ELSE (GOTO UNKNOWN) C:\> run.bat "-b" (does not match, and jumps to UNKNOWN instead of SPECIFIC) 

Using "%~1"=="-b" is the most reliable. %~1 will remove surrounding quotes if they exist. Thus, it works with quotes and without them, as well as without arguments.

 IF "%~1"=="-b" (GOTO SPECIFIC) ELSE (GOTO UNKNOWN) C:\> run.bat C:\> run.bat -b C:\> run.bat "-b" C:\> run.bat "ab" (all of the above tests work correctly) 
+2
Oct 25 '17 at 21:38
source share



All Articles