Shell – Attempt to hide console using Command line, Run, Exec and Pipe in VBScript

command lineshellvbscript

I'm using Zbarimg which (I believe) outputs its data to STDOUT. So at present, a cmd string within a VBScript executes the command and stores it in a variable, thus:

xml_data=objShell.Exec("C:\MobiEvent\Zbar\bin\zbarimg -D -q --xml C:\MobiEvent\AllPics\*.jpg").stdout.readall

(For the uninitiated, -D stops it displaying an image of the barcode it reads from image, and -q keeps things quiet so only the XML data is output) That all works well with all the data of AllPics being stored in xml_data. Problem is, as ever, the console pops up a window that stays open whilst the script processes. And with 200+ photos, it stays open a while

The alternative is to use the Run command, and tag ",0,False" onto the end … but then of course, the StdOut output is not picked up for post processing!

I'm wondering if it could be possible to combine the two using a pipe, my THEORETICAL syntax being …

objShell.Run("C:\MobiEvent\Zbar\bin\zbarimg -D -q --xml C:\MobiEvent\AllPics\*.jpg"|to other script),0,True

** Not even sure if the program would do that as it may read the pipe as part of the directory path

Which then beggars the next question. If it =is= possible to pipe out the STDOUT, how do I read that piped output back into a second VBScript program? (All I need to do is get the output of the program to print out to a file so that I can post process by reading in again and splitting it into component parts to derive the photo filename and the QR value)

I know this "Run=hide window / Exec=cannot hide" debate has raged for a while. Just wondered if the above MIGHT prove an alternative option.

Best Answer

I've no idea about your specific piece of software, but you can pipe and you can redirect in vbScript using the .Run command. For example:

' demonstrate piping and redirecting output with .Run
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTemp=objShell.ExpandEnvironmentStrings("%TEMP%") & "\"
strCmd=objShell.ExpandEnvironmentStrings("%comspec%") & " /C"

' here is the action
objShell.Run strCmd & "echo hello and welcome|findstr /N welcome >" & strTemp & "test.txt",0, True

' read the file back to show it was created successfully
Set file = objFSO.OpenTextFile(strTemp & "test.txt", 1)
strTaskText = file.ReadAll
strTaskText=Left(strTaskText,Len(strTaskText)-2) ' remove trailing vbCrLf
file.Close

MsgBox "The output was:" & vbcrlf & strTaskText
Related Question