1
votes

Alright, I've spent a few hours bumming around forums and google trying to write this script but I think I'm too much of a noob or doing something too abnormal to Frankenstein my code.

I'm trying to run and executable that scans other files and gives me feedback and prompts. Manually you run this in the command prompt, it does an initial scan, asks what you want to do, you run a repair (could take 5+ min) and then it prompts to run again or close.

Pseudo Code should be:

  • Run EXE ArchiveFile ConfigFile
  • Wait for "Enter a command>"
  • Type 1
  • Wait for "Enter a command>"
  • Type 7
  • Wait for "Enter the print destination>"
  • Type 2
  • Wait for "Enter a command>"
  • Type 9
  • Done

Here is my actual code

'Cycle through each iha file in the chosen folder
for each objFile in objFSO.GetFolder(archiveFolder).files
If LCase(objFSO.GetExtensionName(objFile.Name)) = "iha" Then
    'run the iharchiveInfo.exe with the chosen config and the current archive
        msgbox(chr(34) & exeFile & chr(34) & " " & chr(34) & objfile.path & chr(34) & " " & chr(34) & ArchiveConfig & chr(34) )
        set oExec = Shell.exec("cmd.exe /k "& chr(34) & exeFile & " " & chr(34) & objfile.path & chr(34) & " " & chr(34) & ArchiveConfig)
        do while Not oExec.StdOut.AtEndOfStream
            MsgBox oExec.stdout.Readline
        Loop
        Msgbox("second line")
        oExec.stdin.write chr(34) & exeFile & " " & chr(34) & objfile.path & chr(34) & " " & chr(34) & ArchiveConfig
        Do While oExec.Status = 0
            WScript.Sleep 100
            WScript.StdOut.Write(oExec.StdOut.ReadAll())
            WScript.StdErr.Write(oExec.StdErr.ReadAll())
        Loop
        msgbox ("write")
        oExec.stdin.write 1
        do while Not oExec.StdOut.AtEndOfStream
            MsgBox oExec.stdOut.Readline
        Loop
        oExec.stdin.write 7
exit for
end if
next

It opens cmd prompt but I never get a msgbox with the Readline. Instead, when I close the cmd prompt I then get msgbox Write.

1
In a command prompt you use cscript.exe to run vbscripts as opposed to the non command prompt wscript.exe. Wscript is the default program for vbs scripts. So you need to specify what you want cscript //nologo "c:\script.vbs". See cscript /? for help. - user6017774
I'm using wscript atm. I'm a bit confused by the differences and have been debating with myself which one is appropriate. More confused by msdn site that uses wscript in the example and then says it only works with cscript and wscript will give you an error. msdn.microsoft.com/en-us/library/skwz6sz4(v=vs.84).aspx - Furbs
Cscript is a console program. Wscript is a GUI program. Console programs automatically get a console. - user6017774
I really just need to know how to get the cmd to show text! Its completely blank but for a while I had it showing text and responses but I dont know why! I added \k < con but I cant find any documentation on what < con means/does. Now I'm not using the \k anymore, so I guess the < con doesnt work? IDK. thats the main thing stopping my debugging and making me rely on this forum... - Furbs
See my list here of punctuation at the console stackoverflow.com/questions/31820569/…. In short you need to run your program with cscript.exe. - user6017774

1 Answers

0
votes

The first thing you do after starting the command is read the output line-by-line until the end of the stream is reached:

Do While Not oExec.StdOut.AtEndOfStream
    MsgBox oExec.stdout.Readline
Loop

However, that only happens when cmd terminates, which it won't, because you started it with the parameter /k. Thus you created a deadlock. The same goes for ReadAll in the next loop.

Depending on what the actual output is use either ReadLine if you can read entire lines

Do
    line = oExec.StdOut.ReadLine
Loop Until line = "something"

or Read single characters until the condition is met where you want to stop reading, e.g.:

str = ""
Do
    str = str & oExec.StdOut.Read(1)
Loop Until str = "something"

Also, at the point where oExec.Status <> 0 the process has already terminated, so writing to StdIn won't work anymore. You need to put the write operation someplace where the descriptor is still open (i.e. oExec.Status = 0).