Tutorial on Basic Windows 2000 DOS
This Section Will Cover
- 00. What a file is under win enviroment
- 01. What a variable is in %win2k% batch and how to create them.
- 02. What echo echo. @echo off does.
- 03. What operators like > and >> and | do.
- 04. What a ::comment is.
- 05. What & does and how to use it &::creatively.
- 06. What 'DIR' and 'SORT' is and does
- 07. What a * and a ? mean to the DOS shell.
- 08. What GOTO _is when used with :_labels, also using PING to create a delay.
- 09. What if "statements"=="are and what they do".
- 10. What a basic "for" loop is/does.
- 11. How to use low interger values with variables.
- 12. How to parse strings into substrings.
- 13. How to echo non-echoable characters.
- 14. How to pass %arguments to a .bat file.
- 15. Summary
Arguments. Well you know what a switch is right? like DIR /B. The /B is a switch of the command DIR. Now in scripts,
you can use switches too, the command is filename.bat arguments The
arguments are like switches to
commands, almost exactly the same in how they work. Arguments are variables represented by %1 , %2, %3, etc.
Notice you only refer to the value of these variables with a single % infront of the variable name and NOT another
one after the variable name.
How the below works is that it creates a file, and uses the file it creates as argument %1 Now argument %0 you
cannot change. Argument %0 is actually the .bat file name of itself. It represents the file that was started by the cmd.exe.
So %0 is equal to whatever you named your .bat file.
@echo off
IF NOT "%1"=="" GOTO _labelx
SET FILENAME="argument_test"&::
echo Line 1 > %FILENAME%&:: this use's only 1 >, so it overwrites the previous file if you run it again.
echo Line 2 >> %FILENAME%
echo Line 3 >> %FILENAME%
%0 %FILENAME%
GOTO :EXIT
:_labelx
FIND /I /N "LiNE 3" %1
echo.
DEL %FILENAME%&::This command deletes the temporary file, DEL /? for more info about switch's.
::
:: NOTE: since we only use > for the first echo line that will overwrite the file. But that doesn't delete
:: the file, just overwrite's it. So we use DEL to delete the file that it creates whether or not
:: we only ran the script one or several times.
echo Press [ENTER] to exit
pause >nul
Ok the /N switch with FIND just puts the line number infront of which line contained the text it found.
Let's break what might be confusing down into pieces.
NOTE: I put these line numbers below in myself, I didn't use FIND
/N
to do it. Although you could if you're
crafty enough with the FOR loop.
Line 1 @echo off
Line 2 IF NOT "%1"=="" GOTO _labelx
Line 3 SET FILENAME="argument_test"&::
Line 4 echo Line 1 > %FILENAME%&:: this use's only 1 >, so it overwrites the previous file if you run it again.
Line 5 echo Line 2 >> %FILENAME%
Line 6 echo Line 3 >> %FILENAME%
Line 7 %0 %FILENAME%
Line 8 GOTO :EXIT
Line 9 :_labelx
Line 10 FIND /I /N "LiNE 3" %1
Line 11 echo.
Line 12 DEL %FILENAME%&::This command deletes the temporary file, DEL /? for more info about switch's.
Line 13 ::
Line 14 :: NOTE: since we only use > for the first echo line that will overwrite the file. But that doesnt delete
Line 15 :: the file, just overwrites it. So we use DEL to delete the file that it creates whether or not
Line 16 :: we only ran the script one or several times.
Line 17 echo Press [ENTER] to exit
Line 18 pause >nul
Line 2: This checks to see if %1 is empty. Since we started the script normally, and did not pass a argument to
it, there is no %1 so its empty.
Line 3: Sets the filename we will output to, you should already understand this.
Line 7: This is where 1 of 2 argument calls are used. Remember %0 is the filename of the script itself. So
this...
%0 %FILENAME%
translates into...
your_script_name.bat %FILENAME%
and that translates into...
your_script_name.bat "argument_test"
Ok what happens here is that is immediately calls your same script
again but this time passes the
filename of "argument_test" to it as it runs. Ok Line 8 exits the script you started with the
GOTO :EXIT
Now a new script is created with Line 7 and this time when it hits the first line,
IF NOT "%1"=="" GOTO _labelx %1 is no longer empty. %1 is "argument_test" Since %1 is
no longer empty the if statement is true, since %1 is NOT empty, it does GOTO _labelx
Now, it jumps to that label, and performs FIND /I /N "LiNE 3" %1 This is the 2nd argument call.
It finds the text "LiNE 3" in argument %1 %1 is still "argument_test" so it finds that text
in that file.
FIND /I /N "LiNE 3" %1
translates into...
FIND /I /N "LiNE 3" "argument_test"
After it performs find it carries on down the script to perform Line's 11-18
If you want to use all agruements you can do %* That means all arguments. Normally you could only use
%1-%9. If you were to use %1-%9 you could use SHIFT to shift the arguments. SHIFT works like this.
Say you have these files all in the same directory my_script.bat
file1.txt file2.txt file3.txt and you run the command of...
my_script.bat file1.txt file2.txt file3.txt
There are 3 arguments you are passing to my_script.bat. They are
file1.txt, file2.txt, and file3.txt.
Ok, like I mentioned before, you don't have to pass actual files as
arguments. Actually all that is
passed in as arguments are strings/text. But if the string happens to be filename you can perform routines
on it. An argument is just a variable. What you do/want with that
variable is up to you. But know it's
just text that you can mold to whatever you want it to be.
Ok try the below, it uses SHIFT. SHIFT just takes %1 %2 %3 and shifts the arguments to the left. Like waiting
in line to use a restroom. Everytime someone gets done, the line shifts 1 spot closer.
@echo off
IF NOT "%1"=="" IF NOT "%2"=="" IF NOT "%3"=="" GOTO _labelz
SET /A arg_count=0&::
SET foo=Arg_1&::
SET fop=Arg_2&::
SET fos=Arg_3&::
%0 %foo% %fop% %fos%
IF "%1"=="" IF "%2"=="" IF "%3"=="" GOTO :EXIT
:_labelz
echo :-:-: %1 :-:-:
SET /A arg_count=%arg_count%+1&::
SHIFT
IF "%1"=="" GOTO _end
GOTO _labelz
:_end
echo.
echo SHIFT shifted %arg_count% times
echo.
echo Only the %%1 was used, but shift let us see them all.
echo.
echo Press [ENTER] to exit
pause >nul
You can see I chain IF statements, if you were to not pass in
at least 3 arguments, it would continually loop.
Why would it continually loop? Figure that out for yourself.
SHIFT is the key word. Everytime that command is encountered it
moves the arguments down the line. Once it gets
done moving the arguments down the line, it will SHIFT the last
valid argument out of range and %1 will equal "" nothing. That
is when IF "%1"=="" GOTO _end evaluates to true and it goes to the
label :_end.
Something you should notice/note: The number of arguments is dictated by spaces. Each argument you want
must not have spaces in it. That is why I used Arg_1, Arg_2, Arg_3
instead of Arg 1, Arg 2, Arg 3. If you were to remove
the underscore from the argument variables, and replace it with a space, the system would see
6 arguments instead of 3. That is because each variable, foo, fop, and fos have a space in each
of them. The script would run but the output would look like this...
:-:-: Arg :-:-:
:-:-: 1 :-:-:
:-:-: Arg :-:-:
:-:-: 2 :-:-:
:-:-: Arg :-:-:
:-:-: 3 :-:-:
SHIFT shifted 6 times
Only the %1 was used, but shift let us see them all.
Press [ENTER] to exit
That's all I have to say about arguments. Obviously these are used A LOT
in
the real world.
Click here to continue
|