- Batch Script - Logging
- Batch Script - Debugging
- Batch Script - Printing
- Batch Script - Network
- Batch Script - Registry
- Batch Script - Devices
- Batch Script - Aliases
- Batch Script - Process
- Batch Script - Functions
- Batch Script - Return Code
- Batch Script - Input / Output
- Batch Script - DATE & TIME
- Batch Script - Operators
- Batch Script - Decision Making
- Batch Script - Arrays
- Batch Script - Strings
- Batch Script - Comments
- Batch Script - Variables
- Batch Script - Syntax
- Batch Script - Files
- Batch Script - Commands
- Batch Script - Environment
- Batch Script - Overview
- Batch Script - Home
Batch Script Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Batch Script - Input / Output
There are three universal “files” for keyboard input, printing text on the screen and printing errors on the screen. The “Standard In” file, known as stdin, contains the input to the program/script. The “Standard Out” file, known as stdout, is used to write output for display on the screen. Finally, the “Standard Err” file, known as stderr, contains any error messages for display on the screen.
Each of these three standard files, otherwise known as the standard streams, are referenced using the numbers 0, 1, and 2. Stdin is file 0, stdout is file 1, and stderr is file 2.
Redirecting Output (Stdout and Stderr)
One common practice in batch files is sending the output of a program to a log file. The > operator sends, or redirects, stdout or stderr to another file. The following example shows how this can be done.
Dir C: > pst.txt
In the above example, the stdout of the command Dir C: is redirected to the file pst.txt.
If you append the number 2 to the redirection filter, then it would redirect the stderr to the file psts.txt.
Dir C: 2> pst.txt
One can even combine the stdout and stderr streams using the file number and the ‘&’ prefix. Following is an example.
DIR C: > psts.txt 2>&1
Suppressing Program Output
The pseudo file NUL is used to discard any output from a program. The following example shows that the output of the command DIR is discarded by sending the output to NUL.
Dir C: > NUL
Stdin
To work with the Stdin, you have to use a workaround to achieve this. This can be done by redirecting the command prompt’s own stdin, called CON.
The following example shows how you can redirect the output to a file called psts.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 psts.txt.
TYPE CON > psts.txtAdvertisements