Windows – Passing command-line arguments to a windows batch script


I need to create a windows batch script where I need to pass a few command-line arguments. These arguments may appear in in any order, but the script should pick them up properly based on the argument name.

<scriptname> -age <agevalue> -gender <gender>
<scriptname> -gender <gender> -age <agevalue>

The script can be called in any of the two above ways but the age and gender should be properly picked up and assigned to variables. The arguments can be identified by the switch before them – i.e., -age , -gender, etc.

Can someone please give me an example of identifying this internally ?

Best Answer

batch, unfortunately, doesn't have a builtin getops function like does. However, you could implement your own poor-man's variant:

 if /I %~1 == --age set AGE=%2& shift
 if /I %~1 == --gender set GENDER=%2& shift
if not (%1)==() goto GETOPTS