08 May 2012

The Ultimate Guide To Batch File Programming PART- 8



Redirection in Batch Programming:

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.batch-file-programming 8

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 NULL 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 < symbol. It is most commonly used to send the contents of a text file to DOS. The other common usage of this feature is the MORE command which displays a file one screen at a time unlike the TYPE command which on execution displays the entire file.(This becomes impossible to read as the file scrolls by at incredible speed.)Thus, many people send the long text file to the MORE command by using the command:

c:\windows>more < xyz.txt

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: < xyz.bat

ABOUT THE AUTHOR
AbhiShek SinGh
Founder of 'TheHackingArticles'. Cyber Security Analyst, Cyber Security Researcher, and Software Engineer. Follow 'AbhiShek SinGh' on Facebook , Twitter or Google+ or via Email

Subscribe to stay up to date