Batch File Execution - Redirect Command (CMD) Screen Output to a File (All Windows)

Windows8LOGO_200x67.jpg

MS_MVP_89x36.gif
Batch File Execution
Redirect Command (CMD) Screen Output (stderr) to a File


I've always enjoyed using batch files for certain tasks as it saves me time and is relatively easy. Debugging batch scripts often involves reviewing the output generated on the CMD screen itself, which is not always easy -- especially if you have ECHO enabled (@echo on) and have long sub-directory names which results in the text wrapping, depending on your CMD "Property" settings.

You can make debugging via CMD screen information easier by redirecting the CMD screen output (aka "stderr") to a file by executing your batch file via CMD line command using the following format:
Code:
 [COLOR=#ff0000]batch file[/COLOR][COLOR=#000000] >[/COLOR] [COLOR=#003366]CMD screen output file name[/COLOR] [COLOR=purple]2>&1[/COLOR]

Code:
[COLOR=#ff0000]YourBatchFile.bat[/COLOR] > [COLOR=#003366]"%userprofile%\documents\CMDScreenOutput.txt"[/COLOR] [COLOR=#800080]2>&1[/COLOR]

As you know, the red and blue file names are variable; the 2>&1 is constant.

So now instead of scrolling through and trying to discern the wrapped-line output of the command shell screen, you can do so in Notepad or your favorite text editor. :)

Some of you may know the CMD screen output as stderr and the more commonly known and used redirection output (via > or >>) as stdout, which reminds me a little of my IBM mainframe JCL days (SYSABOUT, SYSPRINT, SYSOUT, SYSUDUMP, SYSDBOUT). :)




Additional Information
Ignoring the output of a command - Mark Yocom's Script Tips - Site Home - MSDN Blogs
Hidden features of Windows batch files - Stack Overflow
Command Redirection, Pipes | Windows CMD | SS64.com
Batch files - Redirection (Rob van der Woude's Scripting Pages)
 
Last edited:
Back
Top