Using batch file input parameters

Many programs are able to handle input parameters, which one can specify when launching the program from the command line. This usually makes the program more generic and a better tool to work with (Ex. giving "a:" as parameter):

Format a:

When starting a batch file, then one can also specify input parameters. The parameters can be accessed by the batch file like environment variables, and have the values %1 to %9.

To check if a parameter is given:

IF "%1"!="" ECHO Got %1

More Info MS KB71247

Note there also exists the input parameter %0, which is actually the batch-filename it self.

Note one can also use the SHIFT command incase the input parameter doesn't have to be in a certain order. SHIFT moves the parameter one to the left so %1 disappears, %2 becomes %1, %3 becomes %2 and etc. This behavior can be useful when having a batch file which receives an undefined order or amount of parameters.

:PARSELOOP
IF "%1"=="" GOTO ENDPARSE
IF "%1"=="DEBUG" SET BUILD=DEBUG
IF "%1"=="RELEASE" SET BUILD=RELEASE
SHIFT
GOTO PARSELOOP
:ENDPARSE

Updated: 23 September 2007

Comments:

  1. Edwin Duncan-Dunlop says:

    I have a list.txt file of filenames,which I want to use 1 at a time as parameters in a batch file.

    I thought I could use

  2. Jai Brown says:

    I am trying to help a friend out.
    He has a batch chatroom that echo's a line of text to another batch file to ban users..

    echo if %computername% == %BanUser% taskkill /im cmd.exe>>BannedUser.bat

    The problem is the code above just sends the name of my computer.
    how can I actually port the variable %computername% to BannedUser.bat?

  3. CmdCrazy says:

    set testvar=computername

    echo if %%testvar%%==%BanUser% taskkill /im cmd.exe>>BannedUser.bat

Leave a Reply

Your email address will not be published. Required fields are marked *