Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

[Tutorial] Arguments

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 😯)
Quote: %~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:
Quote: %~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:
Code
Shift /#

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

Code
@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.
Thanks mate! I've been wondering how to do this for a while now and could never figure it out. Great post.
-- "The quieter you become, the more you can hear"
No problem!
Rem This post is too short.
(Aug 5, 2010 08:51 PM)White Light Wrote:  Rem This post is too short.

Lmao




Thanks, This is a great tutorial. If people haven't read this they should. (Diamond in the rough thread, *Let's wipe the dust off*)
[Image: ed8URKg.jpg]


thanks for the tutorial, i will try, is awesome what you can do with batch!
Bumping this =)
People should see this.
[Image: 8vvxbk8.jpg]
Bumping again. Using this for my batch text document encryptor/decryptor =)
[Image: chain.png]
Hmm, I never saw this. @_@ Nice one Ice Cream, if you're still interested in what people think about this thread. :P
I'm always interested in what people think about my threads.
Awesome tutorial bro. It really helped me
[Image: 8vvxbk8.jpg]
(Apr 5, 2012 07:09 AM)GL1TCH3D Wrote:  Awesome tutorial bro. It really helped me

Thanks for saying it. I'm really glad I can help you out.
Thanks man you broaden my batch scope
You always make some of the best tutorials
Meh, if anyone pays attention to my threads and bothered to read >THIS< It tells how to use it, no offence White Light.
(Nov 16, 2012 05:34 PM)WOLGA Wrote:  Meh, if anyone pays attention to my threads and bothered to read >THIS< It tells how to use it, no offence White Light.

What do your threads and that page have in common?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Gliese 667CcAug 28, 2021 11:08 AM 3 192 Aug 28, 2021 11:08 AM
Last Post: Sierra-117
[.NET] by VUEDUEJul 11, 2021 06:45 AM 4 209 Jul 11, 2021 06:45 AM
Last Post: VUEDUE
[C] by ShallowMESep 24, 2020 07:47 PM 4 151 Sep 24, 2020 07:47 PM
Last Post: Sajuuk