Saturday, January 14, 2017

Listing files & directories in given folder using node.js synchroniously

Here is my code for getting list of directories & sub-directories & files within each of it for given path.

var fs = require("fs");
var path = require('path');
var op = new Object();

var basePath = '/home/test_user/pictures/samples/';

function myCb(opt){
    if(op[opt.path] == null){
        op[opt.path] ={
            files:null,
            directories:null
        }
        op[opt.path].files = opt.files
        op[opt.path].directories = opt.dirs
    }

    for(var j=0;j< opt.dirs.length;j++){
        walkSync(opt.dirs[j],myCb);
    }
}

function walkSync(searchPath,cb) {
    var pathList = fs.readdirSync(searchPath);

    var fileList=[];
    var dirList=[];

    for (var i = 0; i < pathList.length; i++) {
        var walkPath = path.join(searchPath, pathList[i]);
        var stat = fs.statSync(walkPath);
        if (stat.isFile()) {
            fileList.push(walkPath);
        }
        else if (stat.isDirectory()) {
            dirList.push(walkPath);
        }
    }

    var output ={
        "path":searchPath,
        "files":fileList,
        "dirs":dirList
    }

    cb(output);
}

walkSync(basePath,myCb);
console.log(JSON.stringify(op));

output is an array of object of type

"source_folder_path" : 


{
    "/home/test_user/pictures/samples/": {
        "files": [],
        "directories": [
            "/home/test_user/pictures/samples/1",
            "/home/test_user/pictures/samples/2"
        ]
    },
    "/home/test_user/pictures/samples/1": {
        "files": [
            "/home/test_user/pictures/samples/1/IMG_1942067a4_235124.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a4_235138.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213012.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213020.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213022.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213034.jpg"
                ],
        "directories": []
    },
    "/home/test_user/pictures/samples/2": {
        "files": [
                      "/home/test_user/pictures/samples/2/IMG_20151218_232553.jpg"
        ],
        "directories": [
            "/home/test_user/pictures/samples/2/4"
        ]
    },
    "/home/test_user/pictures/samples/2/4": {
        "files": [
            "/home/test_user/pictures/samples/2/4/IMG_1942067a8_000623.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_1942067a8_000626.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420123_200014.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420123_200018.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420206_267a022.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420214_215517.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420214_215650.jpg"
        ],
        "directories": []
    }
}

No comments:

Post a Comment