Tuesday, September 30, 2008

Batch File Programming Part-7

Redirection:
Normally the Output is sent to the screen(The standard STDOUT)and the Input is read from the Keyboard(The standard STDIN). This can be pretty boring. You can actually redirect both the Input and the Output to something other than the standard I/O devices.
To send the Output to somewhere other than the screen we use the Output Redirection Operator, > which is most commonly used to capture results of a command in a text file. Say you want to read the help on how to use the net command, typing the usual Help command is not useful as the results do not fit in one screen and scroll by extremely quickly. So instead we use the Output Redirection operator to capture the results of the command in a text file.

c:\windows>net > xyz.txt
This command will execute the net command and will store the results in the text file, xyz.txt . Whenever DOS comes by such a command, it checks if the specified file exists or not. If it does, then everything in the file is erased or lost and the results are stored in it. If no such file exists, then DOS creates a new file and stores the results in this new file.
Say, you want to store the results of more than one command in the same text file, and want to ensure that the results of no command are lost, then you make use of the Double Output Re Direction Symbol, which is the >> symbol.

For Example:
c:\windows> net >> xyz.txt
The above command tells DOS to execute the net command and append the output to the xyz.txt file, if it exits.DOS not only allows redirection to Files, but also allows redirection to various devices.

DEVICE NAME USED DEVICE:
AUX Auxiliary Device (COM1)
CLOCK$ Real Time Clock
COMn Serial Port(COM1, COM2, COM3, COM4)
CON Console(Keyboard, Screen)
LPTn Parallel Port(LPT1, LPT2, LPT3)
NUL NUL Device(means Nothing)
PRN Printer

Say for example, you want to print the results of directory listings, then you can simply give the following command:

c:\windows>dir *.* > prn
The NUL device(nothing) is a bit difficult to understand and requires special mention. This device which is also known as the 'bit bucket' literally means nothing. Redirection to the NUL device practically has no usage
but can be used to suppress the messages which DOS displays on the completion of a task.
For example:
when DOS has successfully copied a particular file, then it displays the message: '1 file(s) copied.'
Now say you want to suppress this task completion message, then you can make use of the NUL device.

c:\windows>copy file.txt > NUL
This will suppress the task completion message and not display it.

Redirecting Input:
Just like we can redirect Output, we can also redirect Input. It is handled by the Input Redirection Operator,which is the < style="font-weight: bold;"> c:\windows>more <>
This command sends the contents of the xyz.txt file to the MORE command which displays the contents page by page. Once the first page is read the MORE command displays something like the following on the screen: ......MORE...... You can also send key strokes to any DOS command which waits for User Input or needs User intervention to perform a task. You can also send multiple keystrokes.
For example:
a typical Format command requires 4 inputs, firstly pressing Enter to give the command, then Disk Insertion prompt, then the VOLUME label prompt and lastly the one to format another disk. So basically there are three User inputs-: ENTER, ENTER N and ENTER.(ENTER is Carriage return)So you can include this in a Batch file and give the format command in the following format: c:\windows>format a: <>.
In the next post we will finish our Batch File Programming Concepts.

No comments: