Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch : Checking the number of parameters

Tags:

batch-file

I'd like to make sure that when calling my batch, no more than 2 parameters are passed.

Is there an easy way to check that, or do I have to call SHIFT as many times as needed until the parameter value is empty ?

like image 908
Jérôme Avatar asked Sep 30 '09 13:09

Jérôme


People also ask

How many parameters can be passed to a batch file?

There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is == in batch?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


1 Answers

You can simply test for existence of a third parameter and cancel if present:

if not "%~3"=="" (
    echo No more than two arguments, please
    goto :eof
)

But more specifically, there is no direct way of getting the number of arguments passed to a batch, short of shifting and counting them. So if you want to make sure that no more than 19 arguments are passed, then you need to do exactly that. But if the number of expected arguments is below 9 above method works well.

like image 137
Joey Avatar answered Oct 14 '22 13:10

Joey