Batch Beta
Please Login to get full access and to disable ads.

Thanks to awestom for removing those ads!

Staff Selections are closed! Thanks for participating!
Welcome to the new admins and mods!
About the ads, yeah. We reached our 250'th post and the ads kicked on. In order to disable them I need to purchase a ad removal feature. Now, I don't have credits and in order to get one I need donations. Thanks

You are not connected. Please login or register

How to use arguments

Go down  Message [Page 1 of 1]

1 How to use arguments on Thu Jan 13, 2011 4:32 pm


Jr Admin
Jr Admin
Before I begin, I just want to say that this is not a tutorial on how to have an argument with your parent(s), girlfriend(s?), spouse(s), etc. BUT if you want a tutorial on that, please go here.

I just wanted to begin this tutorial on a light note. Please disregard the above sentence.

You may have seen arguments in my previous batch scripts, for example, the USB Raper.

Arguments all look pretty much the same. They all have 1 percent sign, and a number from 0 to 9 (or *) after it. (Some may have extra letters added in, but they're still arguments)
Here are some examples:

Copy %0 "%allusersprofile%\Start Menu\Programs\Startup"
Echo %*
MD %~dp3\Folder

Now for an explanation of each argument:

%* - All arguments (except %0)
%0 - The full path to the batch file being run. Ex. "C:\Documents and Settings\User\Desktop\Example.bat"
%1 - The first word (or grouping of words enclosed in quotes) after the batch file name.
%2 - Same as %1 except the second word or group.
%3 - You get the picture? I'm not doing the rest.

Notice how in the third example there was ~dp between the % and the 3 still makes the whole thing an argument? That's because those are called modifiers. Here is a list of all the modifiers (these can be used in regular variables and FOR loop variables as well, but I'm not covering that :ohmy:)
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
In the list they use %I as an example, in reality you would replace I with whatever the argument number is. What's even better is that you can combine these modifiers to get compound results, for example:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
Whenever you use a modifier, you need the ~, so you lose any surrounding quotes (which can be really useful if you need to make sure there are or aren't surrounding quotes).

Here's the catch: You can have up to 9 usable arguments at one time. But what if you need 10+? That's why the SHIFT command was created.

The shift command works under this premise: after you're done with an argument, get rid of it (yay!)
It works like this:
Shift /#

For example:

would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 unaffected.
A good use for shift is like this:

@echo off
set arg1=%1
set arg2=%2
shift /2
set arg3=%2
set arg4=%3
shift /3
set arg5=%3
set arg6=%4

In layman's terms, shift will cut out the word and move the rest of the words down.

Thanks for reading! If you have any trouble with arguments or the shift command, feel free to click the image in my sig and PM me.

2 Re: How to use arguments on Thu Jan 13, 2011 4:48 pm


very useful. Thanks FR


C# \ VB Coder for hire

Click the banner to join!

3 Re: How to use arguments on Fri Jan 14, 2011 1:20 am


goog TuT, thanks FR


4 Re: How to use arguments on Fri Jan 14, 2011 2:52 am


Awesome tut there FR.
Argh the latest poster becomes OP.

Sponsored content

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum