This guide assumes that you know a little bit about dos including
basic commands, the basics of batch files and the use of autoexec.bat.
If you are *nix user, you will recognise some of the techniques
that I describe in this document. I decided to write this guide
because I have never seen these tips in print, and I find them very
useful and valuable. I hope you will too!
TIP: Typing '/?' after most commands will display a help page about
that command. Try 'dir /?'.
Echo
You will probably know that you can display messages on the screen
by using 'echo'. But it is more versatile than it would first appear.
You can turn off the prompt, (the 'c:\windows\>' bit) by typing
'echo off'. You can display a blank line by typing 'echo.'. And
finally, if you put '@echo off' at the top of a batch file, the
commands from the batch file will not be displayed on screen.
Environment Variables
These are named pieces of data that are accessible by all programs.
(Including windows programs, VB programmers can use the 'GetEnvironmentVariable'
API call to get environment variables). To display all environment
variables you use the program that you use to set them, 'set'.
If you type 'dir /?' you will get the help page for this command.
Take a look at the line that says 'Switches may be preset in the
DIRCMD environment variable'. Dir automatically sorts files and
folders by date, but I prefer to see the folders first, then the
files ordered by name. Normally I would type 'dir /OGN /P', but
by using the DIRCMD environment variable, I can set these as the
default parameters with the command 'set DIRCMD=/OGN /P'. Now when
I type 'dir', it will automatically use the parameters that I stored
in DIRCMD! (I put 'set DIRCMD=/OGN /P' in autoexec.bat so I don't
have to type it each time I load DOS.)
The Path Environment Variable
When you type 'set', all the variables are displayed, including
one named 'path', which contains a list of directories. These directories
are the ones that are searched when you type in a command. For example,
when you use fdisk or format, you can be in any directory when you
use them even though they are stored in c:\windows\command. They
are made available by the inclusion of c:\windows\command in the
path variable.
If you want to add a new path to the path variable, and type 'set
path=c:\games', you will notice that your directory has replaced
all the paths that were in the variable previously. You can avoid
this by typing 'set path=c:\games;%path%'. What is going on here?
Well, a word enclosed in percentage signs represents a variable.
Therefore you are making path contain your path, and the previous
contents of path. To see an example, try 'echo %path%' and the contents
of path will be displayed.
TIP: there is a program called 'path' that you can use the set
the path variable.
Prompt
Have you ever wondered to yourself, "Wouldn't it be cool to
be able to change the way the command prompt looks like"? No,
me neither, but it is possible. If you type 'set', you will see
a line that looks like 'PROMPT=$p$g'. This tells command.com that
the prompt should show the path and a right facing chevron. Try
changing it around by typing 'set PROMPT=$g$p' and you will get
something that looks like '>c:\windows'. There are a whole series
of special things you can put into the prompt, for example you can
display the current time with '$T'. For a complete list, type 'prompt
/?'. (Prompt is a program similar to path, in that it provides and
alternative way of changing the prompt variable.)
PRANKSTER TIP: Find a friend's computer and set the prompt to 'c:\windows\$g'
so it will look like a normal prompt, but will not change when you
friend tries to change directory!
The Insertion Operator.
If you have ever used C++ you will recognise the insertion operator
'>>'. You probably be surprised that you can use a version
of this in the DOS environment! It is used to redirect the output
of a program to a text file instead of the screen. You may want
to use it if the output of a command overflows the screen, or you
want to print the output. Try 'dir /s >> files.txt'. This
will output all files in the current directory and all subdirectories,
but append this output to a file named 'files.txt'. You can also
use 'dir /s > files.txt'. Notice that in this example there is
only one chevron. This will perform as before, but if there is any
text in 'files.txt' it will be overwritten.
There is another method of redirecting the output of a command.
If you have used a *nix system you will probably be familiar with
the pipe '|' symbol. Well, this can also be used in DOS. The most
useful application of this is when used with 'more', a program used
to display text one page at a time. Try 'dir /s | more' This will
be similar to typing 'dir /s /p' (It will display the contents of
the current directory and all subdirectories, one page at a time.)
Other useful redirections include sending the output to 'sort' and
'find'.
TIP: If using pipe produces a 'too many parameters' error, try
replacing the pipe with the Alt+1,2,4 character.
Doskey
Ever used the Unix shell tcsh? There is a program in DOS, called
Doskey, which enables some similar operations. It allows the user
to scroll through previous typed commands and edit them without
having to delete back to the mistake. It also enables frequently
used commands to be stored as macros! This is a very handy tool
(used by default on WinNT), especially if you use DOS frequently.
TIP: add 'doskey /insert' to autoexec.bat to load it by default.
For more information including using macros, type 'doskey /?'.
Further Reading
Take a look at 'batricks' by Laurence Soucy for many more cool tips.
http://home7.inet.tele.dk/batfiles/intro/contents.htm
|