Geeks With Blogs
David Douglass .NET on My Mind

You can run a DOS batch script from Windows explorer by double clicking on it, but the output scrolls by and then the window disappears.  What if you need to see the output?  The best thing to do is create a shortcut to the script that captures the output.  For example, given a script test.bat in C:\Program Files\Test, the target of the shortcut should be:

"C:\Program Files\Test\test.bat" > test.txt 2>&1 & test.txt

The "> test.txt" sends the output to a file; the "2>&1" sends any error messages to wherever the non errors messages are going (test.txt in our case); and the "& test.txt" launches test.txt as a command once the DOS script completes.  The net effect is that you can double click on the shortcut, the DOS script will run, and the output will be displayed using Notepad or whatever .txt is mapped to.

A variation on this is to use || (2 pipe bars) in place of the last ampersand, in which case the output file is launched only if the script returns an error code.

Posted on Saturday, July 3, 2010 6:14 PM | Back to top

Comments on this post: The Easy Way to Run a DOS Batch Script

# re: The Easy Way to Run a DOS Batch Script
Requesting Gravatar...
Cool David! Thanks for the excellent tip.
Left by iceman on Jan 17, 2013 9:02 PM

Your comment:
 (will show your gravatar)

Copyright © David Douglass | Powered by: