0

I am trying to keep some backups in a folder. I want to keep a backup of 30 JSONs at any time, JSONs are being added hourly. If the number of files in the folder is > 30 I want to delete the older files. Is this possible with nodejs?

This is what I've got so far:

ipcMain.on("BACKUP_TO_DISK", function(event, arg) {    
    var date = new Date();
    var json = JSON.stringify(arg);
    fs.writeFile("./backup/" + date.yyyymmddhh()+ "_" + arg.prefix + "_backup.json", json, "utf8", function() {
      console.log("Successfully Saved.");
    });
  });

and its being called from my React app like so:

this.interval = setInterval(() => {
      var data = this.props.data.get("data");
      var prefix = this.props.preferences.get("savePrefix");
      var counter = this.props.data.get("counter");
      var path = this.props.preferences.get("path");

      console.log("BACK")
      this.props.backupToDisk(data, prefix, counter, path);  
    }, 60000 * 30 ); //every half an hour
R. Kohlisch
  • 2,823
  • 6
  • 29
  • 59

1 Answers1

0

Yes, you can use filesystem to do that. These two should be enough to build such thing: https://www.w3schools.com/nodejs/nodejs_filesystem.asp How do you get a list of the names of all files present in a directory in Node.js?

Doğancan Arabacı
  • 3,934
  • 2
  • 17
  • 25