Validating parameters in batch files dating customs of china
Also keep in mind that labels cannot contain delimiters (space, comma, semi-colon, etcetera), they must be unique, and that only the first 8 characters are used (so the first 8 characters must be unique! This technique is best used when each valid value for %1 has its own batch code to process it: NUL IF ERRORLEVEL 1 ( ECHO Invalid argument: %1 ECHO.
i have a batch file which should take inputs "-input1", "-input2" and "-input3".
Keep in mind, though, that these "features" may vary with the operating systems used.
The batch file's limitation to handle parameters up to I know of several occasions where these seemingly useless "features" proved very handy.
The following trick uses ANSI to perform some key translation: the Enter key is translated to the F6 key followed by the Enter key. OS/2 users may want to take a look at User In PM, a utility written in VX-Rexx, displaying a small PM window asking for user input.
Thus only one line of input can be entered, and pressing the Enter key sends the input to the temporary file USERINP. It creates a temporary batch file to set an environment variable to the typed input.
There are various hacks for creating counters which you can find here, but be warned these are not for the faint hearted.
@echo off setlocal enableextensions enabledelayedexpansion call :getargc argc %* echo Count is %argc% echo Args are %* endlocal goto :eof :getargc set getargc_v0=%1 set /a "%getargc_v0% = 0" :getargc_l0 if not x%2x==xx ( shift set /a "%getargc_v0% = %getargc_v0% 1" goto :getargc_l0 ) set getargc_v0= goto :eof It basically iterates once over the list (which is local to the function so the shifts won't affect the list back in the main program), counting them until it runs out.
This executable understands command line arguments and will call the validator engine (now in a DLL) to perform the correct job.
It also uses a nifty trick, passing the name of the return variable to be set by the function.
The main program just illustrates how to call it and echos the arguments afterwards to ensure that they're untouched: @echo off setlocal Enable Extensions Enable Delayed Expansion set /a arg_idx=1 set "curr_arg_value=" :loop1 if !
So I've looked around, and either I'm looking in the wrong spot or I'm blind, but I can't seem to find a way to get a count of number of command line arguments passed in.
Is there a command similar to shell's "$#" for batch files? the closest i've found is to iterate through the %1s and use 'shift', but I need to refernece %1,%2 etc later in the script so that's no good. If you have more than 9 arguments then you are screwed with this approach though.