How to Redirect Output to a Text File From Within a Batch File

Siddharth Bishnoi Feb 02, 2024
  1. Redirection Operator in Batch Script
  2. Redirect Output Using the Redirection Operator in Batch Script
  3. Redirect the Output to a Text File by Calling a Subroutine From Within a Batch File
How to Redirect Output to a Text File From Within a Batch File

This tutorial will teach the different ways of redirecting the output from within a text file.

Redirection Operator in Batch Script

We can redirect the output of a batch file to a text file using the redirection operator. A redirection operator redirects the input to the command or output from the command.

When running a Batch file, the command is executed in cmd.exe. The output of these commands is obtained in two streams, i.e., standard output and standard error.

These outputs can be either redirected to separate files or a single file.

A redirection operator is denoted by >. By default, the cmd uses > for the standard output, the same as 1>.

However, it uses 2> for the standard error. The basic syntax of a redirection operator is given below.

command > filename

Redirecting output to a text file can be very useful. When there’s a very long range of output or a command is executed in an interval of time, the data must be saved.

Also, if an error occurs while running a Batch file, it quickly disappears with a blank console screen. In all cases, the output and the error can be redirected to a text file.

When using a Batch file, it is better to redirect the output from within the Batch file. So, whenever you run it by double-clicking on it, it will redirect the output, which is better than doing it manually every time from the command line.

Redirect Output Using the Redirection Operator in Batch Script

We can redirect the standard output of the entire Batch file or just a part of it. Also, the standard output and standard error can be saved in a single file or separate files.

Redirect the Standard Output to a Text File From Within a Batch File

To redirect the standard output to a text file, add the redirection operator between the command and the text file, as shown in the syntax below.

command > filename

For example, we have to redirect the output of the command powercfg to a text file named stdoutput.txt. The following command will create a new file called stdoutput.txt.

If the file already exists, it will be overwritten.

echo "The output will be redirected to a text file"
powercfg /a > stdoutput.txt

testfile redirection

Output:

output redirection

The > operator overwrites the existing file with the new output when you run the Batch file. To keep the old output and append the new output, use >> instead of > to append the output to the same text file.

echo "The output will be redirected to a text file"
powercfg /a >> stdoutput.txt

testfile append

Output:

output append

Redirect Standard Output and Standard Error to a Separate Text File From Within a Batch File

To redirect the standard output (stdout) and standard error (stderr) to separate text files, use 1> for standard output and 2> for standard error, as shown below.

@echo off
echo "The output will be redirected to stdoutput.txt"
powercfg /a 1> stdoutput.txt
echo "The errors will be redirected to stderror.txt"
powercfg /energy 2> stderror.txt

testfile redirect to separate files

Standard output:

output stdoutput

Standard error:

output stderror

Redirect All Outputs to a Single File From Within a Batch File

We can save the standard output and standard error in a single file using 2>&1 after the file name.

The syntax for this is shown below:

@echo off
echo "The output will be redirected to stdoutput.txt"
powercfg /a > stdoutput.txt 2>&1

testfile redirect to a single file

Output:

output redirect to a single file

Similarly, to append both the outputs to a single file, use >> instead of > in the above command.

ifconfig >> output.txt 2>&1

testfile append redirect to a single file

The output will be the same as there are no errors in the file.

Redirect the Output to a Text File by Calling a Subroutine From Within a Batch File

Another way to redirect the output of a Batch file to a text file is by using the call and sub commands. An example of calling a labeled subroutine is given below.

@echo off
call:sub_ipconfig > ipconfig.txt
call:sub_powercfg > powercfg.txt

GOTO: END

:sub_ipconfig
ipconfig
EXIT /B

:sub_powercfg
powercfg /a
EXIT /B

:END

testfile call sub

Output- ipconfig.txt:

output call sub ipconfig

Output- powercfg.txt:

output call sub powercfg

Here, the call command is used to call the subroutine as labeled. First, the subroutine sub_ipconfig is called, and the output is redirected to a file named ipconfig.txt.

Then the second subroutine sub_powercfg is called, and the output is redirected to a file named powercfg.txt. The command exit /B stops the execution after this line and continues running the main program.

If we use exit instead of exit /b, it will stop the execution of the Batch file.

So, we have discussed two different ways to redirect the output within a Batch file.