Redirection


Click here to change the theme.

Redirection is a mechanism for a process to supply input to and/or output from another process; in other words, the input and/or output of another process is redirected. Redirection is often called piping. In a Windows environment, redirection applies to Console ("Command Prompt") windows. Redirection works only for files that use Standard Input and Standard Output. Not all Console programs use Standard Input and Standard Output, so redirection will not work for all Console programs. The following is a sample of executing a script, writing to the script's input and reading from the script's output.

Dim WshShell, oExec, RedirectedScript
Set WshShell = CreateObject("WScript.Shell")
RedirectedScript = """C:\Documents and Settings\Sam\My Documents\Projects\Scripts\VB\"
RedirectedScript = RedirectedScript & "Redirected.vbs"""
Set oExec = WshShell.Exec("CScript " & RedirectedScript)

oExec.StdIn.WriteLine "One"
oExec.StdIn.WriteLine "Two"
oExec.StdIn.Close		' AtEndOfStream for redirected

While Not oExec.StdOut.AtEndOfStream
	WScript.StdOut.WriteLine (oExec.StdOut.ReadLine)
Wend

' I don't know if the following is necessary
While oExec.Status <> 1
	WScript.Sleep 100
Wend

Be sure to change the RedirectedScript variable as needed. The following script can be used to test the above.

While Not WScript.StdIn.AtEndOfStream
	WScript.StdOut.WriteLine WScript.StdIn.ReadLine
Wend