Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run a windows batch file from node.js

am trying to run a test.bat file inside node.js

here is the code

var exec = require('child_process').execFile;

case '/start':
    req.on('data', function (chunk) {});
    req.on('end', function () {
      console.log("INSIDE--------------------------------:");      
       exec('./uli.bat', function (err, data) {
        console.log(err);
        console.log(data);
        res.end(data);
      });
    });
    break;

while running this node.js file am getting

INSIDE--------------------------------:
{ [Error: Command failed: '.' is not recognized as an internal or ext
nd,
operable program or batch file.
] killed: false, code: 1, signal: null }
like image 297
Sush Avatar asked Mar 27 '14 05:03

Sush


People also ask

How do you run a batch file in JavaScript?

var wshShell = new ActiveXObject("WScript. Shell"); wshShell. Run("D:\\dir\\user. bat");

How do I run a batch file from a website?

Just create a batch file and save in the location where your html file is there. this anchor tag will execute the (test. bat) batch file. After clicking on the link <TEST>, you will get the window prompting to open/save/close, if you will click on open then batch file will be executed.


1 Answers

I have found the solution for it.. and its works fine for me. This opens up a new command window and runs my main node JS in child process. You need not give full path of cmd.exe. I was making that mistake.

var spawn = require('child_process').spawn,
ls    = spawn('cmd.exe', ['/c', 'my.bat']);

ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});

ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
like image 83
SoftwareGuy Avatar answered Sep 21 '22 10:09

SoftwareGuy