Ad

Node.js Split Large File Into Parts And Iterate Over The Parts

- 1 answer

Node.JS running on linux. Test for files larger than 100MB and split them into chunks of 100MB. I can use exec

// executes `split`
child = exec("split -d -b 104857600k  $file_name", function (error, stdout, stderr) {
  sys.print('stdout: ' + stdout);
  sys.print('stderr: ' + stderr);
  if (error !== null) {
    console.log('exec error: ' + error);
  }
});

But how do I know how many parts were generated and how can I iterate over them?

Ad

Answer

You can enable --verbose to print in stdout result files.

child = exec("split -d -b 104857600k --verbose $file_name", function (error, stdout,stderr) {
  if (!error) {
    // Get only file names from the output 
    var resultFiles = stdout.match(/x(\d+)/g);
  } else {
    console.log('exec error: ' + error);
  }
}); 
Ad
source: stackoverflow.com
Ad