stdout - Capturing output from WshShell.Exec using Windows Script Host -


i wrote following 2 functions, , call second ("callandwait") javascript running inside windows script host. overall intent call 1 command line program another. is, i'm running initial scripting using cscript, , trying run else (ant) script.

function readallfromany(oexec) {      if (!oexec.stdout.atendofstream)           return oexec.stdout.readline();       if (!oexec.stderr.atendofstream)           return "stderr: " + oexec.stderr.readline();       return -1; }  // execute command line function.... function callandwait(execstr) {  var oexec = wshshell.exec(execstr);   while (oexec.status == 0)  {   wscript.sleep(100);   var output;   while ( (output = readallfromany(oexec)) != -1) {    wscript.stdout.writeline(output);   }  }  } 

unfortunately, when run program, don't immediate feedback called program doing. instead, output seems come in fits , starts, waiting until original program has finished, , appears have deadlocked. want have spawned process share same stdout calling process, don't see way that. setting oexec.stdout = wscript.stdout doesn't work.

is there alternate way spawn processes share stdout & stderr of launching process? tried using "wshshell.run(), gives me "permission denied" error. that's problematic, because don't want have tell clients change how windows environment configured run program.

what can do?

you cannot read stderr , stdout in script engine in way, there no non-blocking io code master bob says. if called process fills buffer (about 4kb) on stderr while attempting read stdout, or vice-versa, deadlock/hang. starve while waiting stdout , block waiting read stderr.

the practical solution redirect stderr stdout this:

scommandline = """c:\path\to\prog.exe"" argument1 argument2" dim oexec set oexec = wshshell.exec("cmd /s /c "" " & scommandline & " 2>&1 """) 

in other words, gets passed createprocess this:

cmd /s /c " "c:\path\to\prog.exe" argument1 argument2 2>&1 " 

this invokes cmd.exe, interprets command line. /s /c invokes special parsing rule first , last quote stripped off, , remainder used as-is , executed cmd.exe. cmd.exe executes this:

"c:\path\to\prog.exe" argument1 argument2 2>&1 

the incantation 2>&1 redirects prog.exe's stderr stdout. cmd.exe propagate exit code.

you can succeed reading stdout , ignoring stderr.

the downside stderr , stdout output mixed together. long recognisable can work this.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -