Batch Script Input and Ouput
Input and Output
There are three universal "files" for keyboard input, screen text printing, and screen error printing.
- The Standard Innput file, known as
stdin, contains the input to the program/script.
- The Standard Output file, known as
stdout, is used to write the output to be displayed on the screen.
- The Standard Error file, known as
stderr, contains all the error messages to be displayed on the screen.
Each of these three standard files, otherwise known as the standard streams, are referenced using numbers:
stdinis file 0
stdoutis file 1
stderris file 2
A common practice in batch scripting is to send the output of a program to a log file.
You use the
> operator to send, or redirect, stdout or stderr to another file. The following example shows how this can be done.
In the following example, the
stdout of the command
dir C:\ is redirected to the file list.txt.
dir C:\ > list.txt
If you append the number
2 to the redirection filter, then it would redirect the
stderr to the file lists.txt.
dir C:\ 2> list.txt
You can even combine the
stderr streams using the file number and the
dir C:\ > lists.txt 2>&1
Suppressing Program Output
NUL is used to discard the output of a program.
The following example shows that the output of the
dir command is discarded by sending it to
dir C:\ > NUL
To work with the
stdin, a workaround must be used. This can be done by redirecting the command prompt
The following example shows how you can redirect the output to a file called lists.txt. After you execute the below command, the command prompt will take all the input entered by user till it gets an
EOF character. Later, it sends all the input to the file lists.txt.
TYPE CON > lists.txt