Arguments in Batch

Batch files Arguments (in my opinion) are the substring commands that you specify when you call the batch file. To show you in an understanding way take this for example:

In the command line you enter this command:

call bat1.bat Hello

And bat1.bat contained this code:

echo %1
pause

The outcome would output to the cmd screen:

Hello
Press any key to continue. . .

%1 is the first argument you specify that is placed after the filename. These arguments are separated by spaces (unless grouped together by double quotes). So with each argument specified the number increases to %2 and %3 and so on. %0 is the name of the batch file you are calling or starting.

The syntax works like this; compare:

%0 %1 %2
BatchFile1.BAT FirstArgument SecondArgument

[u][b]Second Part – More Clarifying[/b][/u]

This batch file will list the first two arguments and its own filename:

@echo off
echo.
echo The name of this file is %0
echo Argument 1: %1
echo Argument 2: %2
echo.
pause

From the command line or another batch file if you call this batch file with these arguments:

call BatchFile1.BAT Hello World

Then you should get an output like this (except for maybe the filename):


The name of this file is BathFile1.BAT
Argument 1: Hello
Argument 2: World

Press any key to continue. . .

As you can see it got it’s own filename(%0) and Hello was the first argument (%1) and World was the second argument (%2).

But what if we combined “Hello” and “World” into one argument using double quotes? Well if we did do that:

Call BatchFile1.BAT "Hello World"

Then it will treat that as one whole argument or %1 in this case and output this:


The name of this file is BatchFile1.BAT
Argument 1: "Hello World"
Argument 2: 

Press any key to continue. . .

Since the second argument was not specified, then the value of %2 is Null; therefore the echo request of %2 echoes nothing to the screen.

[u][b]Third Part – Drag and Drop
[/b][/u]
Now lets say you have two separate batch files on your desktop.

bat1.bat contained this code:

@echo off
echo %1
echo.
call %1

and bat2.bat contained this code

@echo off
echo Hello World!
pause

If you would drag and drop bat2.bat into bat1.bat you would be opening bat1.bat with an argument which is the full filename of bat2.bat
With the code of bat1.bat it would take that first argument, echo it to the screen, then it would call that file as denoted by “call %1”.
Here are the actual results:


"%userprofile%\Desktop\bat2.bat"

Hello World!
Press any key to continue. . .

In application, this batch file will delete the file that is dropped into it:

@echo off
del /f /q %1
exit

[u][b]Fourth Part – The [color=#00BFFF]SHIFT[/color] Command[/b][/u]

There is a way to move the positions of the arguments in a way with the SHIFT command. SHIFT moves each argument one index down from its original position.
So if you had these arguments:

%1 = Candle
%2 = Network
%3 = Beach
%4 = Book

and you would use the single word SHIFT as the command the outcome would move the argument values to this:

%0 = Candle
%1 = Network
%2 = Beach
%3 = Book

You could say that it will literally shift %4 to %3, %3 to %2, %2 to %1, and %1 to %0. The original %0 (filename) would be gone (unless you saved the value of %0 into another variable before the SHIFT command) and %4 would be empty.

Makes sense? Try it out.

Now if you don’t want SHIFT to affect some of the arguments, you can specify the command to start shifting at a specified argument index. This specified index can be between 0 and 8.
For example, if you want your args to be shifted down to the 2nd argument but leave %1 and %0 unaffected then type:

SHIFT /2

Jokingly, I guess you could say that the number you specify for SHIFT is the argument that will have its value deleted.

So when applying it to:

%0 = {FileName}
%1 = Candle
%2 = Network
%3 = Beach
%4 = Book

The outcome of “SHIFT /2” would be:

%0 = {FileName}
%1 = Candle
%2 = Beach
%3 = Book

SHIFT may come in handy to you someday. Keep it in mind.

That’s my explanation on Batch File Arguments.

5 thoughts on “Arguments in Batch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: