The two most commonly used redirection operators used are the output redirection operator and the input redirection operator. The output redirection operator > is used to send the command output to somewhere other than the screen. A text document would be an example. An example would be wanting to put the results of the
DIR command into a text document. Let us name the text file dir.txt:
When this command is entered either at a Windows DOS prompt or in a batch file you do not see the directory listing like you would if you had simply entered the
DIR command. Instead, a file is created and in this case the file is named dir.txt. This can be used to capture practically any command.
Run this file several times and notice the changes. One might think that each time you run this program (
DIR >dir.txt), each result of the
DIR command gets saved to that dir.txt file. Well, each time you run that program you will erase the preceding contents and save the new
DIR listing information. What if we wanted to create a type of log that would append and save each new occurrence of the above program? We can use what is called the append redirection operator. This is done by adding another greater than sign.
Next we have the input redirection operator. The input redirection operator is used to send the contents of a file to a DOS command (normally the contents of keyboard input get sent to the DOS command). You have probably already guessed that, since the output redirection operator is
>, the input redirection operator will be a
<. That would be a correct guess.
An example of when you would want to do this would be if you have a text file that is too large for the Windows command prompt screen. You can enter the following command sequence:
DIR listing was very long than you would be prompted to
Press any key to continue... after a screen-full amount of data was displayed. Once you pressed (almost) any key you would see the next screen-full amount of data. If it was short than the
MORE command will simply display the text as if you typed the
TYPE command. The
TYPE command is used to display the contents of a file. You do not need to use a redirection operator to use the
This will produce the contents of dir.txt to screen.
The next command we will learn is the DOS piping command. The pipe
| can usually be inserted to the command prompt by holding down the SHIFT key and pressing the key to the direct left of the backspace key. DOS piping is a technique that combines both the output and input redirection operators. We can capture the output of one command and send it as the input to another command. Example:
C:\>dir | more
In this example the pipe command captures the output of the
DIR command and sends it as input to the
MORE command which will display one screen-full of results at a time.
You will come to find that batch scripting really does have a great amount of power and has wonderful uses. However, there will be a time when you will find that batch will have just not quite enough power. That is the price we pay for having such a nice and easy to learn tool at our hands.
Please do not hesitate to contact our support department with any possible further questions or to solve practical issues connected with Command line email and batch files.