Passing command-line arguments to a windows batch script

6,723

Solution 1

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

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

Solution 2

Can you do it with Powershell? or does it need to be batch? Powershell makes it nice and easy, you just need to add something like this at the top of your script, and then you can call each parameter in any order

param (
[string]$Age,
[string]$Gender
)

You can do it with batch, but it will require lots of parameter checking.. Here's a few links of some batch scripts that do it

http://www.ericphelps.com/batch/samples/arguments.bat.txt

(Possible dupplicate of)

https://stackoverflow.com/questions/5215729/is-there-a-way-to-pass-parameters-by-name-and-not-by-order-to-a-batch-bat-f

Share:
6,723

Related videos on Youtube

Dhiwakar Ravikumar
Author by

Dhiwakar Ravikumar

QA Automation Engineer with over 7 years of experience. I worked on delivering several features and products to customers with a high degree of quality. I have designed automation frameworks from scratch & automated several test cases to help boost productivity and increase test coverage while simultaneously improving QA standards. I used to work at Commvault and now I work at Cohesity.

Updated on September 18, 2022

Comments

  • Dhiwakar Ravikumar
    Dhiwakar Ravikumar almost 2 years

    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. E.g.:

    <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 ?

  • Dhiwakar Ravikumar
    Dhiwakar Ravikumar almost 10 years
    this might be cumbersome as the number of arguments exceeds but it fits the bill. Thanks
  • Dhiwakar Ravikumar
    Dhiwakar Ravikumar almost 10 years
    it has to be batch scripts only :(. We have a strict requirement that it be a batch script. Thanks a lot though :)
  • Axel Kemper
    Axel Kemper almost 10 years
    You might want to add the double quotes. Otherwise, the statement does not work with empty parameter %1.
  • Ravindra Bawane
    Ravindra Bawane over 9 years
    If your various possible arguments have a set format or type, such as age (being numeric) and gender (being "M" or "F") you shouldn't have to define the arguments, but instead use comparison recognition to assign them to the appropriate variables within the script. I did this for an early batch script I wrote during a transition period between 9x and XP at my first IT job. The script could either accept arguments for the OS type, the drive the script was running from, and a few other things, or it could auto-detect the appropriate settings. A bit overkill, but a fun exercise.