Q. How can a Vbscript process each line in the STDOUT generated from a CMD command or batch?

The following script is an example of how a Vbscript can process each line in the STDOUT generated by a CMD.EXE command or batch.

To run the Example.vbs script, type:

cscript //nologo <Drive:>\Folder\Example.vbs "command or batch"

Examples

cscript //nologo c:\util\Example.vbs "dir /b /ad"

cscript //nologo c:\util\Example.vbs "c:\util\test.bat Jerry"

Example.vbs contains:

Dim objArguments
Set objShell = WScript.CreateObject("WScript.Shell")
Set objArguments = Wscript.Arguments
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c " & objArguments(0))
Do While Not objExecObject.StdOut.AtEndOfStream
    strText = objExecObject.StdOut.ReadLine()
' Test or display strText
    Wscript.Echo strText    
Loop


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish