var fs =require('fs')//遍历文件夹,获取所有文件夹里面的文件信息/* * @param path 路径 * */functiongeFileList(path){var filesList = []; readFile(path,filesList);return filesList;}//遍历读取文件functionreadFile(path,filesList){ files = fs.readdirSync(path);//需要用到同步读取 files.forEach(walk);functionwalk(file) { states = fs.statSync(path+'/'+file);if(states.isDirectory()) { readFile(path+'/'+file,filesList); }else {//创建一个对象保存信息var obj =new Object(); obj.size = states.size;//文件大小,以字节为单位 obj.name = file;//文件名 obj.path = path+'/'+file;//文件绝对路径 filesList.push(obj); } }}//写入文件utf-8格式functionwriteFile(fileName,data){ fs.writeFile(fileName,data,'utf-8',complete);functioncomplete() { console.log("文件生成成功"); } }var filesList = geFileList("G:/nodejs");filesList.sort(sortHandler);functionsortHandler(a,b){if(a.size > b.size)return -1;elseif(a.size