Question

I am using the Node FFI module and am trying to run sync tasks on Windows. I can successfully run a task using the following code.

var ffi=require('ffi')
var nativeC = new ffi.Library("Kernel32", {
"WinExec": ["int32", ["string"]]
});

nativeC.WinExec('ls -lrt');

I presume this is the way to execute sync tasks, but this code always exits after the 1st 'ls -lrt' command; if I chain a few more commands, they won't work. So, is there a callback function over here, in the FFI module, or another way I can chain commands in node.js on Windows so they run in sync, one after the other.

Was it helpful?

Solution

I'm not sure you need WinExec to run a windows command. As Jonathan pointed out, ls isn't available.

However, if you want to chain commands you could use async.js and exec like this:

var
  async = require('async'); 
  exec = require('child_process').exec,
  commands = [ 'dir /w', 'echo test'];

var executeCommand = function(command, callback){
  exec(command, function (err, stdout, stderr) {
    if(err) return callback(err);
    console.log(stdout);
    callback();
  });
};

async.eachSeries(commands, executeCommand, function(err){
  console.log('error: ' + err);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top