Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: PATH Environment Variable (locations to search)[Next]: Windows Registry Files
\->Introduction->Redirection of STDIN, STDOUT & STDERR Input/Output Streams

Redirection of STDIN, STDOUT & STDERR Input/Output Streams

First of all what are these streams?:

The special characters "<" and ">" are used to indicate redirection.

As well as a Return Code most programs you will execute from the command line will pass information back to you for you to "read", for example if you execute the following command:

DIR

It would display a list of the files in the current directory.

If you wanted a list of ".gif" files to email to someone you could use the command:

DIR *.gif > DirectoryOutput.txt

Using NotePad (or the "TYPE" command) you could check that the file "DirectoryOutput.txt" actually contains the listing.

In the above command we "redirected" the "stdout" handle, if there we no ".gif" files then you would still have seen an error message generated and the "DirectoryOutput.txt" file would be empty!

The following adds redirection of the stderr steam to a "nul" device which has special meaning, it will be ignored and won't appear anywhere:

DIR *.gif > DirectoryOutput.txt 2>nul

If you wanted "stderr" redirected to the "same place" as the stdout handle then you could do any one of the following commands (many variations):

#1: DIR *.gif  >  DirectoryOutput.txt  2> DirectoryOutput.txt
#2: DIR *.gif 1> "DirectoryOutput.txt" 2> DirectoryOutput.txt
#3: DIR *.gif 1>  DirectoryOutput.txt  2>&1

After using any one of the above 3 commands you would always have something in the "DirectoryOutput.txt" file (either date and/or error messages).

In the above examples I used ">" to redirect the output to a file, the file is created or overwritten (if it already exists), to append to the file you would use ">>" instead.

It is also possible to "pipe" the output of one command into another.

The following demonstrates directing a file's contents into a command

sort.exe < SomeFile.txt > SortedContents.txt 2>&1

The problem with redirection is the user doesn't see the output, you could redirect to temporary files which you then "TYPE" or you could use one of the many "TEE.EXE" programs around.


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: PATH Environment Variable (locations to search)[Next]: Windows Registry Files


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.