English 中文(简体)
Batch Script - Input / Output
  • 时间:2024-11-05

Batch Script - Input / Output


Previous Page Next Page  

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.txt
Advertisements