Tuesday, November 22, 2011

How To Redirect Command Output to a File

Many Command Prompt commands, and DOS commands for that matter, are executed not just to do something, but to provide you with information. The ping command, dir command, tracert command, and several others come to mind when I think of popular commands that produce a lot of data in the Command Prompt window.
Unfortunately, three hundred lines of information from the dir command doesn't do you much good as it rushes by. What if you want to actually look at it, or send it to a tech support group, or use it in a spreadsheet, etc.?
This is where a redirection operator becomes very useful. Using a redirection operator, you can redirect the output of a command to a file. In other words, all the information that's displayed in the Command Prompt after running a command can instead be saved to a file which you can open in Windows to reference later or manipulate however you like.
While there are several redirection operators, which you can read in detail about here, two in particular are used to output the results of a command to a file: the greater-than sign, >, and the double greater-than sign, >>.
The easiest way to learn how to use these redirection operators is to see some examples:
ipconfig /all > mynetworksettings.txt
In this example, I save all the network configuration information that I'd usually see on screen after running ipconfig /all to a file by the name of mynetworksettings.txt.
As you can see, the > redirection operator goes between the ipconfig command and the name of the file I want to store the information in. If the file already exists, it'll be overwritten. If it doesn't already exist, it will be created.
ping 10.1.0.12 > "C:\Users\Tim\Desktop\Ping Results.txt"
Here, I execute the ping command and output the results to a file by the name of Ping Results.txt located on my desktop, which is at C:\Users\Tim\Desktop. I wrapped the entire file path in quotes because there was a space involved.
Remember, when using the > redirection operator, the file I specify is created if it doesn't already exist and is overwritten if it does exist.
ipconfig /all >> \\server\files\officenetsettings.log
This example uses the >> redirection operator which functions in much the same way as the > operator, only instead of overwriting the output file if it exists, it appends the command output to the end of the file.
So let's say the first time you use this command is on Computer A. The officenetsettings.log file is created and the result of ipconfig /all on Computer A is written to the file. Next you run the same command on Computer B. This time, however, the result is added to the officenetsettings.log so the network information from both Computer A and Computer B are included in the file.

As you might have already realized, the >> redirection operator is really useful when you're collecting similar information from multiple computers or commands and you'd like all of that data in a single file.

No comments:

Post a Comment