0

Cant really understand what I do wrong here. Everything loops fine in the console.log but in the file I only get the last one. I quessing it is overwriting the file everytime?

readFiles('/Volumes/Staccs_Sled_001/Eagle Rock 2 Oktober - Exported', (filepath, name, ext, stat) => {

console.log('file path:', filepath);
console.log('file name:', name);
console.log('file extension:', ext);
console.log('file information:', stat);

const infotext = [
    ext,
    filepath,
]

fs.writeFileSync('./exportedTitles.json', JSON.stringify(infotext, null, 2), err => {
  if (err) {
      console.log(err);
  } else {
      console.log('files made');
  }

})

})

Any suggestions what I doing wrong?

blytung
  • 425
  • 1
  • 3
  • 12
  • Use writable streams. https://stackoverflow.com/questions/3459476/how-to-append-to-a-file-in-node/43370201#43370201 – Rupjyoti Nov 03 '20 at 14:33

1 Answers1

2

fs.writeFileSyncoverwrites the file.

You will have to use fs.appendFile

fs.appendFile('./exportedTitles.json', 'data to append', err => {
  if (err) {
      console.log(err);
  } else {
      console.log('files made');
  }
});
kg99
  • 746
  • 2
  • 14