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);
        } else {
          file = file.replace(folder, '')
          results.push({file, changetime: stat.ctime, size: stat.size});

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

const folder = process.argv[2];
  console.log(`File does not exists!`);

const stats = fs.statSync(folder);
  console.log(`File is not directory`);

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

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

  const fileName = `${folderName}.${Math.floor(}.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",
    "changetime": "2019-04-17T11:46:55.381Z",
    "size": 1887
    "file": "/1 - create margin range",
    "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: