Hack Forums
[Shell] [Tutorial] Arguments - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] [Tutorial] Arguments (/showthread.php?tid=592361)

Pages: Pages 1 2


[Tutorial] Arguments - White Light - Aug 5, 2010

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.


* - Dr.Kosm0s - Aug 5, 2010

Thanks mate! I've been wondering how to do this for a while now and could never figure it out. Great post.


* - White Light - Aug 5, 2010

No problem!
Rem This post is too short.


* - iGnite - Feb 11, 2011

(Aug 5, 2010 08:51 PM)Gulfstream 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*)


* - f0rk - Feb 12, 2011

thanks for the tutorial, i will try, is awesome what you can do with batch!


* - GL1TCH3D - Nov 27, 2011

Bumping this =)
People should see this.


* - Sajuuk - Jan 22, 2012

Bumping again. Using this for my batch text document encryptor/decryptor =)


* - BobFM - Jan 22, 2012

Hmm, I never saw this. @_@ Nice one Ice Cream, if you're still interested in what people think about this thread. :P


* - White Light - Jan 22, 2012

I'm always interested in what people think about my threads.


* - GL1TCH3D - Apr 5, 2012

Awesome tutorial bro. It really helped me


* - White Light - Apr 5, 2012

(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.


* - CyberCyan - Nov 16, 2012

Thanks man you broaden my batch scope


* - The Old One - Nov 16, 2012

You always make some of the best tutorials


* - WOLGA - Nov 16, 2012

Meh, if anyone pays attention to my threads and bothered to read >THIS< It tells how to use it, no offence White Light.


* - White Light - Nov 16, 2012

(Nov 16, 2012 05:34 PM)Qwertyuiopasdfghjklzxc 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?