Reputation: 830
i tried to get exe file callback result when i doing shell execute like this:
var oShell = new ActiveXObject("WScript.Shell");
var args = folderName + "\\dir\\scan.exe scan " + params.join(" ");
var ret = oShell.Run(args ,0 ,true);
but ret gaves me 0 for fail and 1 for success. when i run the file in the cmd like this:
scan.exe arg1 arg2 arg3
this is return the correct result that i wanted : "test/test" and not 1...
what can i do?
tnx a lot
Upvotes: 1
Views: 3473
Reputation: 1574
Using a shell to receive output from BATCH file:
script.js
var pathToFile = "your_path_here"
var shell= new ActiveXObject("WScript.shell");
var output = shell.Exec(pathToFile + 'example.bat');
var response = output.StdOut.ReadLine();
console.log(response)
example.bat
@ECHO OFF
echo Hello From Batch World
exit 0
Upvotes: 0
Reputation: 79
I know I might be a bit late to answer this question but I hope it can still help someone.
The way I achieved it was with the oShell.Exec()
function and not with the oShell.Run()
.
oShell.Exec()
returns an object with a property called StdOut
which acts like a text file, so you can perform ReadLine()
, ReadAll()
, etc.
The problem is that it does not wait for the command to end, so when you run your code, it is very likely that your StdOut
object will be undefined
. You have to add that waiting option on the command itself.
var wshShell = new ActiveXObject("WScript.Shell");
try {
// Excecute the 'cd' command.
wshShell.CurrentDirectory = "C:\\Users";
var execOut = wshShell.Exec('cmd /C start /wait /b cd');
}
catch (e) {
console.log(e);
}
// Get command execution output.
var cmdStdOut = execOut.StdOut;
var line = cmdStdOut.ReadLine();
console.log(line);
The code above will execute a cd
command on the directory C:\Users
and store the output on the line
variable.
I hope this answers the question.
Upvotes: 1