Using batch file input parameters

1 January 2001 by Snakefoot | Comment » | Trackback Off
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 KB Q71247

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:

Comment by Edwin Duncan-Dunlop - 22 January 2009 @ 19:33 Reply

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

Comment by Jai Brown - 23 May 2010 @ 12:08 Reply

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?

Comment by CmdCrazy - 28 September 2011 @ 19:32 Reply

set testvar=computername

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

Leave a comment


NB! Use the Forum for computer help and off-topic questions.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>