/ Hack

Compare folders states with Nodejs

Case: You have large folder and some process that makes changes in it. As there are too many nested folders and files there, manual comparison can take too much time, so you would like to find quicker and easier way.

In this post I would like to share with you my light Nodejs script, that can snapshot folders state. So you can take one snapshot before and one after some action, compare them and find changed files.

const fs = require('fs');
const path = require('path');

var walk = function(dir, done) {
  var results = [];
  fs.readdir(dir, function(err, list) {
    if (err) return done(err);
    var i = 0;
    (function next() {
      var file = list[i++];
      if (!file) return done(null, results);
      file = dir + '/' + file;
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          walk(file, function(err, res) {
            results = results.concat(res);
            next();
          });
        } else {
          file = file.replace(folder, '')
          results.push({file, changetime: stat.ctime, size: stat.size});
          next();
        }
      });
    })();
  });
}

if(typeof process.argv[2] == 'undefined'){
  console.log(`Please provide path to director as the first argument, like:\n./script.js /some/path/`);
  process.exit()
}

const folder = process.argv[2];
if(!fs.existsSync(folder)){
  console.log(`File does not exists!`);
  process.exit()
}

const stats = fs.statSync(folder);
if(!stats.isDirectory()){
  console.log(`File is not directory`);
  process.exit()
}

const pathDesc = path.parse(folder);
const folderName = pathDesc.name;

walk(folder, function(err, results) {
  if (err) throw err;

  const fileName = `${folderName}.${Math.floor(Date.now()/1000)}.json`;
  fs.writeFileSync(`./${fileName}`, JSON.stringify(results, undefined, '  '));
  console.log(`Done. Write folder '${folder}' snapshot to file '${fileName}'`);
});

To execute this script provide path to folder as the first argument like this:

node script.js /way/to/folder

After this, script will recursivly walk through all nested folders and prepare list of files with information about there current size and time of last change.This information would be written to the file in JSON format.

[
  {
    "file": "/1 - create currency dimension.pro",
    "changetime": "2019-04-17T11:46:55.381Z",
    "size": 1887
  },
  {
    "file": "/1 - create margin range dimension.pro",
    "changetime": "2019-04-17T11:46:55.397Z",
    "size": 1899
  },...
]

Later you can compare two such files and find difference. Personally, for this purpose, I use DiffTabs package for sublime text editor:

1