node.js之文件读写模块,配合递归函数遍历文件夹和其中的文件

  • fs.stat会返回文件夹会文件的属性
var fs = require('fs');
var wenwa = function (pathname,callback) {
    fs.stat(pathname,function (err,data) {
        if(err) throw  err;
        else
        {
            if(data.isDirectory())
            {
                var wenheichouwa = function (callback){
                        fs.readdir(pathname,function (err,fileordir) {
                            //console.log(fileordir);
                            callback(fileordir)
                        });
                };
                wenheichouwa(function (data) {
                    for (var i=0;i<data.length;i++)
                    {
                        wenwa(pathname+"/"+data[i])
                    }
                })
            }
            else
            {
                console.log(pathname)
            }
        }
    });
};

猜你喜欢

转载自www.cnblogs.com/saintdingspage/p/11432870.html