run a windows batch file from node.js

前端 未结 4 1349
北恋
北恋 2020-12-05 15:58

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         


        
4条回答
  •  无人及你
    2020-12-05 16:07

    The easiest way I know for execute that is following code :

    require('child_process').exec("path/to/your/file.bat", function (err, stdout, stderr) {
        if (err) {
            // Ooops.
            // console.log(stderr);
            return console.log(err);
        }
    
        // Done.
        console.log(stdout);
    });
    

    You could replace "path/to/your/file.bat" by __dirname + "/file.bat" if your file is in the directory of your current script for example.

提交回复
热议问题