Redirect Output from the Home windows Command Line to a Textual content Report

Probably the most helpful tactics to log and troubleshoot the conduct of instructions or batch jobs that you simply run on Home windows is to redirect output to a document.

Alternatively, there are a couple of alternative ways you’ll be able to redirect command line writes to a document. The choice you select relies on how you need to view your command output.

How Home windows Command Recommended Output Works

While you kind a command within the Home windows console (command prompt), the output from that command is going to two separate streams.

  • STDOUT: Usual Out is the place any usual responses from instructions pass. As an example the usual reaction for the DIR command is an inventory of recordsdata inside of a listing.
  • STDERR: Usual Error is the place any error messages pass if there’s an issue with the command. As an example if there aren’t any recordsdata within the listing, the DIR command will output “Report No longer Discovered” to the Usual Error movement.

You’ll redirect output to a document in Home windows for either one of those output streams.

Redirect Usual Output Write to New Report

There are two tactics you’ll be able to redirect usual output of a command to a document. The primary is to ship the command output write to a brand new document each time you run the command.

To do that, open the command steered and kind:

dir take a look at.exe > myoutput.txt

The > personality tells the console to output STDOUT to the document with the identify you’ve supplied.

While you run this command, you’ll understand that there isn’t any reaction within the command window aside from the mistake that the document doesn’t exist.

It’s because the usual output for the command was once redirected to a document referred to as myoutput.txt. The document now exists in the similar listing the place you ran the command. The usual error output nonetheless shows because it in most cases does.

Be aware: Watch out to change the active directory for the command steered earlier than operating the command. This fashion you’ll know the place the output recordsdata are saved.

You’ll view the usual output that went to the document via typing “myoutput.txt” within the command window. This may open the textual content document to your default textual content document viewer. For the general public, that is normally Notepad.exe.

The following time you run the similar command, the former output document will probably be deleted. A brand new output document will probably be recreated with the newest command’s output.

Redirect Usual Output Writes to the Identical Report

What in case you don’t need to overwrite the similar document? Another choice is to make use of >> quite than > to redirect to an output document. On the subject of this situation, you may kind:

dir take a look at.exe >> myoutput.txt

You’ll see the similar output (the mistake simplest).

However on this case, as a substitute of overwriting the output document, this command appends the brand new output to the present output document.

Each time you run a command and append the output to a document, it’ll write the brand new usual output to the tip of the present document.

Redirect Usual Error To a Report

The similar approach you’ll be able to redirect usual output writes to a document, you’ll be able to additionally output the usual error movement to a document.

To do that, you’ll want to upload 2> to the tip of the command, adopted via the output error document you need to create.

On this instance, you’ll kind the command: 

dir take a look at.exe > myoutput.txt 2> output.err

This sends the usual output movement to myoutput.txt, and the usual error movement to output.err. The result’s that no output movement in any respect will get displayed within the console window.

Alternatively, you’ll be able to see the mistake messages via typing output.err. This may open the document to your default textual content document viewer.

As you’ll be able to see, any error messages from the command are output to the mistake document. Simply as with the usual output, you’ll be able to use >> as a substitute to append the mistake to mistakes from prior to now run instructions.

Redirect All Output Writes to a Identical Report

All the approaches above lead to a couple of recordsdata. One document is for the usual output movement and the opposite is for the usual error movement.

If you wish to come with either one of those outputs to the similar document, you’ll be able to do this too. To do that, you simply want to redirect all output to the similar document the use of the next command.

dir take a look at.exe 1> myoutput.txt 2>&1

Right here’s how this command works:

  • The usual output is directed to the output document recognized via output quantity 1.
  • The usual error output recognized via the quantity 2 is redirected to the output document recognized via quantity 1.

This may append the mistake output to the tip of the usual output.

This can be a helpful approach to see all output for any command in one document. 

Silencing Usual or Error Output Streams

You’ll additionally flip off both Usual Output or Usual Error via redirecting the output to a NUL as a substitute of a document.

The usage of the instance above, in case you simplest need Usual Output and no Usual Error in any respect, you’ll be able to use the next command:

dir take a look at.exe 1> myoutput.txt 2>nul

This may lead to the similar output document as the primary instance above the place you simplest redirected the Usual Output, however with this command the mistake gained’t echo throughout the console. It gained’t create an error log document both.

This turns out to be useful in case you don’t care about any mistakes and don’t need them to turn out to be a nuisance.

You’ll carry out any of the similar output instructions above from inside a BAT file and the output from that line will pass to the output document you specify. This can be a helpful approach to see whether or not any instructions inside of a BAT document had any mistakes once they attempted to run.

Published by Marshmallow

Marshmallow Android is BT Ireland’s Head of Sales for Republic of Ireland domestic multi-site companies, indigenous MNCs and public sector accounts. He is responsible for the direction and control of all sales activity in the region. He has over 10 years management experience from high growth start-ups to more established businesses. He’s led teams in Ireland, India and China across various industries (ICT, On-Line Recruitment, Corporate Training and International Education).