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
NOTE: PING is not related to a IF statement at all, PING is just used to create a delay. It is also
NOT a built-in command as far as I know, never really checked.
Ok, your basic IF statement. Evalutates to a true or false condition.
Here is a rough sketch to demonstrate IF...
IF variableA = variableB then do this command
IF NOT variableA = variableB then do this command
IF /I variableA = variableB disregarding case sensitivity for values...then do this command
Ok copy and paste the below in a new .bat file...
@echo off
SET foo=Hello Batch!
SET fop=hello batch!
echo.
echo %foo% &::foo equals the text of Hello Batch!
echo %fop% &::fop equals the text of hello batch! Notice the difference in case
echo.
echo PING is now creating roughly 3 second delay...
PING 127.0.0.1 >nul &::This pings your own computer to create a rough 3 sec delay.
CLS
echo.
IF "%foo%"=="%fop%" GOTO :_equals
IF "%foo%"=="%fop%" SET foy=They matched on 1st IF statement.
::
IF NOT "%foo%"=="%fop%" SET foz=They did NOT!! match on the 1st IF statement.
::
IF /I "%foo%"=="%fop%" GOTO :_equals
::
echo you will never see this text
echo it will skip past this and the GOTO
echo statement will jump right to the label :_equals
echo Again, you will never see this.
::
::
:_equals
echo.
IF NOT "%foy%"=="" echo %foy%
IF NOT "%foz%"=="" echo %foz%
echo.
echo.
echo Press [ENTER] to exit
pause >nul
Ok, we have seen SET before. This sets 2 variables right off the start, foo and fop.
Then it uses PING 127.0.0.1 to create a delay. That ip address is your computer. You
can always use that ip to test things. CLS clears the screen.
Ok I used 3 IF statements here, IF, IF NOT, and
IF /I.
IF is basic, just does a comparison of the exact values.
IF /I does a comparison, but does it with case insensitivity. So a
ROSE is the same thing as a rose.
IF NOT is self explanatory. If the values do NOT equal, do the command.
Since the first two IF statements do not use the /I switch,
the commands they should
perform will never happen since it will always be false.
Since the 3rd IF statements uses NOT, it can tell that case does NOT match
so the comparison is true so they do their commands. Its command is to SET the variable
foz equal to the text They did NOT!! match on first IF
statement.
The fourth IF statement uses the /I switch. /I
disregards case sensitivity so foo does equal fop.
What happens here is that its command is to GOTO :_equals. As soon as
the IF statement
evaluates to true, which it is true, it immediatly jumps to the label below
the echo
statements I have saying you will never see these. They say that because
you won't. Every
GOTO command looks for a label of some sort, if the label is not
present, you will get
a error and most likely the script will immediately exit.
The 2 IF statements I have in the :_equals label area test
to see if the variable is
emtpy or does not exist. If you read above again, you will see that foy never got SET because its
IF statement to SET it was not true. So it tests if
foy is empty or if it doesn't exist.
Since it never got SET, it doesn't exist so it does not echo out
the contents of foy
since it doesnt have any contents to echo. The last IF statement
evaluates to true because foz did get SET
if you look again.
Last you use see 2 echo.'s. to create two blank lines and pause
>nul to pause the screen.
You can also chain IF statements together (as well as FOR
loops) like the below...
IF /I "%foy%"=="%foz%" IF NOT "%foz%"=="" echo %foz%
If you try to output the contents of a variable using echo
or something else, and
the variable does not exist, you will get output on the screen or in a file of
Echo is off (or Echo is ON if its on).
This is because since the variable does not exist, all the system sees is the
word
echo WITHOUT a period "." after it. Issuing the command echo by
itself returns the state of whether or not if echo is on or off.
In our case, we immediately turn it off with the very top line of @echo off
There is also a EXISTswitch you can use and ELSE method. This checks to see if a file or directory exists. Now in win2k,
its way of checking wether or not a directory exists, is sketchy, I don't
think case sensitivity applies. But here is how I used to do it...
IF /I EXIST c:\winnt\nul (echo Winnt directory EXIST's) ELSE echo Winnt directory does NOT EXIST
Now the entire thing has to be on 1 line, not 2. You can't use &
either because that represents a new line.
It checks to see if there is a directory named c:\winnt If it does NOT EXIST it does whatever the
ELSE statement says to do. You can put another IF statement after ELSE and chain things together.
You will notice the word nul again. This works because it checks
to see if there is a place inside the
directory to put files or directories. If the directoy you check, does not
EXIST then obviously there is no place inside the directory to put files
or directories. I said this is sort of sketchy, well it is. In some rare
cases, \nul will not work, but always try to use it. I have
no clue why, but it just won't work in some very rare cases. In win2k you
don't have to put the \nul there to check if the directory exists
for it to work.
However, if you have a file called the same thing as the directory you're
checking, then it will still work because it
found a valid name. EXIST really is supposed to check for files only,
but it can check for directories only
with this method. Luckily, in windows, you cannot have a file named the same
thing as a directory inside the same directory.
You can try this and see for yourself, it just won't work. Remember
though, without using \nul you wont be
sure if it's a file or directory. So use \nul to ensure its a
directory if that is what you want to check.
Although it might not be imperative that you put the ( ) around the
first thing to do, you need to always put that
there for legibility and clarity.
Click here to continue
|