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.
精彩评论