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 ?
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).
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.
[ == ] (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.
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.
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 shift
ing 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With