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
Ok so you understand what a FOR loop is and does. Now
let's deal with how to make a very primitive counter and
how to parse a string, similar to delims/tokens, but using actual character placement instead of tokens.
Ok first here is a sample of how to create a counter. You use SET with the /A switch to do Arithmetic...
@echo off
SET /A foo=0&::You need to always initiate your number value to something.
echo.
:_loop
SET /A foo=%foo%+1&::
echo %foo%
IF "%foo%"=="5" GOTO _end
GOTO _loop
:_end
echo.
echo Press [ENTER] to exit
pause >nul
The output is...
1
2
3
4
5
Press [ENTER] to exit
You have to SET /A foo=0 or to some value, because you cant add 1 +
"", 1 plus nothing. This will generate an
error. After the value you SET foo to make sure there is NO spaces. Or else this could happen...
1 + "1 " which will generate a error because you can't perform arithmetic
on spaces.
When you set values, anything after the = will be its value. This includes spaces and tabs. It is
better practice in win2k .bat to put an online comment after the value,
like SET /A foo=15&::
This way it does three things.
1. Ensures there is no trailing spaces after the value you set.
2. If you would output the SET /A fo... into another .bat, it might have trailing spaces after it. So the trailing
spaces will be in the comment and not the value.
3. You can spot where you SET variable values easier. Believe me it
helps the readability factor of your script alot.
You can reset a variable to use its own value, Like how I did with SET
/A foo=%foo%+1&:: What happens is that
it references the value in %foo%, then performs the arithmetic of whatever foo is +1, then it assigns
foo to equal it's new value.
Ok let's edit the above and initiate foo to equal something other than 0, lets do 1234. Also, at the very end
after we perform the arthimetic on the variable, let's add a text string
to it.
@echo off
SET /A foo=1234&::
echo.
:_loop
SET /A foo=%foo%+1&::
echo %foo%
IF "%foo%"=="1240" GOTO _end
GOTO _loop
:_end
SET foo=%foo%_Cardo_Street&::
::
:: Notice i did NOT use SET /A in the above, but just plain SET
::
echo.
echo %foo%
echo Press [ENTER] to exit
pause >nul
Ok so %foo%, when the script is done, will equal 1240_Cardo_Street This is fine, but what if later we want
to change the address, say it's a mistake and it's actually supposed to be
1241_Cardo_Street This is where
getting a substring comes in handy.
The 1240_Cardo_Street is one entire string, spaces and all.
Seventeen characters in all. Now to get the substring
we will be working with the variable value in %foo%. We know
foo holds that string, so let's get part of the string. This
time we are just going to SET FOO to equal the string to save space.
@echo off
SET foo=1240_Cardo_Street&::
echo.
echo The value of foo equals: %foo%
echo.
SET foo=%foo:~4%&::
echo.
echo The new value of foo equals: %foo%
echo.
echo Press [ENTER] to exit
pause >nul
Here is a rough interpretation of how this works...
SET variable=open_variable variable_name perform_substring_operation operation_on_variable close_variable
SET variable= % foo : ~4 %
SET variable=%foo:~4%
What the above has done, is take all characters from the fourth position
from the left. When you see the ~ that means
more or less all characters. Notice how we still use SET foo= but then comes %foo:~4% To perform this substring
routing you start the variable name with a % but after the name of the variable you open it up for altering by putting
a : instead of a %. What comes after the altering, the :,
is what the operation does on the variable. After you're done
setting the operation you close the variable by putting a % after the
operation.
Now let's go the other way with it and get all but the LAST 4 characters.
@echo off
SET foo=1240_Cardo_Street&::
echo.
echo The value of foo equals: %foo%
echo.
SET foo=%foo:~0,-4%&::
echo.
echo The new value of foo equals: %foo%
echo.
echo Press [ENTER] to exit
pause >nul
Ok here we see %foo:~0,-4% Ok the ~0 means start at the first character. The first character is 0, not 1.
The 0 position marks the first actual character. In actuality the first number is 0, not 1. Remember when we
learned our numbers, we always started at 1. Well that was wrong. 0 comes before 1 does it not? Is 0 that hard
of a concept to overlook when we learn numbers? I hope not.
Before we carry on here is what SET /? says about the substrings...
%PATH:~10,5%
would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.
%PATH:~-10%
would extract the last 10 characters of the PATH variable.
%PATH:~0,-2%
would extract all but the last 2 characters of the PATH variable.
Don't worry if you don't understand what the help says, best thing to do
is create a .bat and put like ten variations
in it using the substring method and see what it does. I didn't remember
exactly how to do this when typing this,
I had to reference it. It's not something you should memorize at all.
Just understand that you can do the substring
method and fiddle with it until you get what you want.
Ok carrying on, well to fix our typo, remember we want 1241_Cardo_Street not 1240_Cardo_Street we can
use the following operation to get the first 4 numbers into foo, and use what we already know to get all the characters
but the first 4 into a new variable.
@echo off
SET foo=1240_Cardo_Street&::
echo foo's value is %foo%
echo.
SET fop=%foo:~4%&::
echo fop's value is %fop%
echo.
SET fos=%foo:~0,4%&::
echo fos's value is %fos%
echo.
SET /A fos=%fos%+1&::
SET foo=%fos%%fop%&::
echo.
echo.
echo foo's new value is %foo%
echo.
echo Press [ENTER] to exit
pause >nul
What we did was declare fop the way we did before and get all the characters
starting at the 4th position by doing %foo:~4% The new variable here is fos. %foo:~0,4% This takes the characters
starting at the 0 position, and only takes 4 of the characters. So
"%fos%"=="1240". Since fos now equals
1240 we did SET /A to perform arithmetic on it by doing
SET /A fos=%fos%+1&::. At the very end we reset foo to equal
the new string by doing SET foo=%fos%%fop%&::.
Just for demonstration here is how you would get middle characters.
@echo off
SET foo=1240_Cardo_Street&::
SET foo=%foo:~5,5%&::
echo.
echo foo's value is %foo%
echo.
echo Press [ENTER] to exit
pause >nul
What the above does, is start at position 5 (~5), remember position starts at 0 so this is the 6th character of
the string. And it gets 5 characters of the string. So it starts at the
letter C, and gets a total of 5 characters
of the string. Starting at C, and getting 5 characters results in Cardo. The C is included in the 5 characters
to get. It does NOT start at C and get 5 characters after C.
It starts at C and that is the first character to get, then it gets 4 more.
You can also perform IF statements on numbers numerically. Like you
can tell if one value is higher, less, equal, etc... than
another.
Here is part of IF /? that shows us how...
where compare-op may be one of:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
In this operation, dont use "'s around the variable, here is an
example...
@echo off
SET foo=16000&SET fop=%RANDOM%&::
echo.
IF %foo% LSS %fop% (echo %foo% is less than %fop%) ELSE echo %foo% is greater than %fop%
echo.
echo Press [ENTER] to exit
pause >nul
Notice I SET fop=%RANDOM% Using %RANDOM just generates a
number between 0 and 32767 on win2k.
It's really a puzzle on how %RANDOM% works. I know it generates the number from the system clock, but the random
number it assigns is of great difference when using %RANDOM% in a script than it is when you use it
in the shell. Dunno, it's seems goofy.
That's all i have to say about substrings and arithmetic. Remember this
is something you're not going to really remember, your
going to have to reference it if you want to use it.
Click here to continue
|