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 first off, before anything, in DOS if you need these, CD changes directory.
CD "c:\program files" changes to directory c:\program files
CD .. changes back up to the parent directory (goes backwards)
Now open up your new .bat file in notepad. You can right click on the file and hit EDIT.
Hitting EDIT opens it up in notepad. Now add the following lines to it, each line
should be on a seperate line by itself for these demonstartions.
NOTE: At any time you want a batch script to end, you can stop it by pressing CTRL+C
SET foo=Hello Batch!
echo [ENTER] to exit
Save the file, and in windows double click it. Ok line by line on what is going on.
@echo off This turns off echo, always add this to the top unless your debugging.
SET foo=Hello Batch! This creates a variable foo. Within foo you are setting the text to Hello Batch!
echo. This creates a space on the screen between %foo% and top of the screen.
echo "%foo%" This is how you reference the variable foo that you set.
echo. This creates another space. Notice the . after the word echo.
echo [ENTER] to exit This is a custom prompt for pause, dont use it's default.
pause >nul This causes the script to pause without displaying pause's default message.
@echo off turns echo off. If you dont add this in, you will see each line of your script, and the output
of what each line does. So you see the line in the script itself, and what the line does. If you want to see
what i mean change it to @echo on and see what it does. You can turn echo off or on at anytime during
a script. By default, echo is on. That is why you have to turn it off. It doesnt have to be at the begining.
In windows after a variable is SET you can reference what the variable holds by
putting %'s around it. So what happens is that it is going to echo out
to the screen what is inside of foo. In this case it echo's to the
screen "Hello Batch!". Now note, that the "'s are not part of the
variable, those are manually added around the variable. The variable only holds what you
set it to. Notice that this echo does not have a . at the end of it.
echo. means that its a emptly line. You only use echo. if you want to create
a empty line on the screen or in a file.
SET varname= is how you set a variable. Whatever is after the = is what the value of the variable
will be SET to. This includes spaces for the first and last characters of whatever you set it to.
i.e. SET varname= Space At Begining and END In this example there is a space before the text and
at the end of the text, you might not be able to see the space at the end, but its there rest assure.
If you remove the >nul from pause you will see a message saying Press any key to continue . . .
All >nul does is supress the output of it saying that. pause still works the same way, you have to press
a key to continue. Actually though I hide it and add my own because in
reality it's not any key like they would lead
you to believe over there in the M$ world. Try pressing, ctrl,shift,alt,caps lock,num lock,print screen, scroll lock,
pause break to see if any of those keys work to continue on through the
script. They don't, so it's not any key after all is it M$ ?
can use >nul in other creative ways as well.
All that >nul means is nothing. I know it's German for 0. Means you basically send it to the trash
by destroying it instantly. NO, this does NOT go to your "Recycle Bin"
The 3 letters, nul is something the system see's. The reason it doesnt create a file called nul in your
directory is because the system knows to look for that already and has a predetermined
method of handling it.
Ok since we touched on >. Ok > redirect output from DOS screen. You can also output to a file the
same way. Go ahead and in your .bat file right now and change echo "%foo%" to look like
echo "%foo%" > some_file.txt You can also use < but we won't cover
that. < means input instead
Now when you run it, there will be a file named some_file.txt created in the directory where your .bat is
and inside that file will be, you you guessed it, "Hello
Now >> works differently than >. More or less > means to output to file regardless if
it exists or not. You can run your script as many times as you like and will only see
the 1 line of "Hello Batch!" HOWEVER, if you put another >, and make it
echo "%foo%" >> some_file.txt now everytime you run the .bat, it will just add another
line to the file. Thus if you use >> and run the script 5 times, you will see 5 lines
of "Hello Batch!" The > will overwrite any file that exists, and >> just adds it to
a file if it exists. With >>, if the file does not exists, it acts just like > and creates
There is also a pipe character | that you can use to immediately direct output to
another command. I wont get into this much, but after you read this
tutorial try something like the below...
DIR /B *.* | FIND /i ".bat"
The above will take the output of the DIR command, and use it as input for the FIND command.
If there is a file in the directory that has a .bat extension it will find it from what
the output of DIR give's to it.
To unset a variable, you just declare it again without anything, not even a space, after
the equals sign. Below is an example of how to unset a variable. Typically you could just do...
However that's not good enough for me :-P...do the below, next section you will understand.
Click here to continue