How to Use Redirection Operators
While there are several redirection operators, 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: In this example, all the information normally seen on screen after running ipconfig /all, is saved to a file by the name of networksettings.txt. It’s stored in the folder to the left of the command, the root of the D: drive in this case. The > redirection operator goes between the command and the filename. If the file already exists, it’ll be overwritten. If it doesn’t already exist, it will be created. Here, when the ping command is executed, Command Prompt outputs the results to a file by the name of Ping Results.txt located on the jonfi user’s desktop, at C:\Users\jonfi\Desktop. The entire file path in wrapped in quotes because a space involved. Remember, when using the > redirection operator, the file specified is created if it doesn’t already exist and is overwritten if it does exist.
The Append Redirection Operator
The double-arrow operator appends, rather than replaces, a file: 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. Here’s an example of what this LOG file might look like after a command has been exported to it: The » redirection operator is useful when you’re collecting similar information from different computers or commands, and you’d like all that data in a single file. The above redirection operator examples are within the context of Command Prompt, but you can also use them in a BAT file. When you use a BAT file to pipe a command’s output to a text file, the exact same commands described above are used, but instead of pressing Enter to run them, you just have to open the .BAT file.
Use Redirection Operators in Batch Files
Redirection operators work in batch files by including the command just as you would from the Command Prompt: The above is an example of how to make a batch file that uses a redirection operator with the tracert command. The yahootracert.txt file (shown above) will be created on the C: drive several seconds after executing the sample.bat file. Like the other examples above, the file shows everything Command Prompt would have revealed if the redirection operator wasn’t used.