开发者

how to get result from wscript.execute() I need piping

I want to get result from an executing command by the help of wscript say :

wscript.execute("dir c:\")

I need to know how can I access to the results of this or in other words I want to know how can I pipe the result into my script?

rega开发者_C百科rds.


First you need to create a shell object:

var shell = WScript.CreateObject("WScript.Shell");

Then there are two methods to choose from. The easy way uses the Shell Run function and sends the command output to a file, which you subsequently read:

var errorCode = shell.Run("cmd /c dir c:\\ > tmp.txt", 1, true); 
    // (1 -> Display window, true -> wait for command to finish)

Alternatively, it is also possible to capture the command output directly using the Shell Exec function:

var cmdRun = shell.Exec("cmd /c dir c:\\");
while (cmdRun.Status == 0) // wait for the command to finish
{
  WScript.Sleep(100);
}

Exec returns an object representing the process, in this case we access the standard output as a TextStream object.

var output = cmdRun.StdOut.ReadAll();

You might expect that you'd be able to interact with the new process using the returned object and its standard I/O pipes, but that doesn't seem to work in my experience. For example, I can't find a way of reading the standard output that doesn't block the script until the command has finished executing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜