nodejs 遍历目录

var fs = require("fs"),
    path = require("path");

function walk(dir, callback) {
    fs.readdir(dir, function(err, files) {
        if (err) throw err;
        files.forEach(function(file) {
            var filepath = path.join(dir, file);
            fs.stat(filepath, function(err,stats) {
                if (stats.isDirectory()) {
                    walk(filepath, callback);
                } else if (stats.isFile()) {
                    callback(filepath, stats);
                }
            });
        });
    });
}

import fs from 'fs';
import path from 'path';


function walk(dir) {
  return new Promise((resolve, reject) => {
    fs.readdir(dir, (error, files) => {
      if (error) {
        return reject(error);
      }
      Promise.all(files.map((file) => {
        return new Promise((resolve, reject) => {
          const filepath = path.join(dir, file);
          fs.stat(filepath, (error, stats) => {
            if (error) {
              return reject(error);
            }
            if (stats.isDirectory()) {
              walk(filepath).then(resolve);
            } else if (stats.isFile()) {
              resolve(filepath);
            }
          });
        });
      }))
      .then((foldersContents) => {
        resolve(foldersContents.reduce((all, folderContents) => all.concat(folderContents), []));
      });
    });
  });
}

es6版本

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

async function walk(dir) {
    let files = await fs.readdir(dir);
    files = await Promise.all(files.map(async file => {
        const filePath = path.join(dir, file);
        const stats = await fs.stat(filePath);
        if (stats.isDirectory()) return walk(filePath);
        else if(stats.isFile()) return filePath;
    }));

    return files.reduce((all, folderContents) => all.concat(folderContents), []);
}

 

 

 

 3 批量处理文件

const fs = require('fs').promises;
const path = require('path');
const EventEmitter = require('events');

let emitter = new EventEmitter();

async function walk(dir) {
    let files = await fs.readdir(dir);
    files = await Promise.all(files.map(async file => {
        const filePath = path.join(dir, file);
        const stats = await fs.stat(filePath);
        if (stats.isDirectory()) return walk(filePath);
        else if(stats.isFile()) return filePath;
    }));

    return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
var walker=walk('.');


walker.then(function(files){
    files.map(function(file){
        if(file.indexOf('.vmCmp')<0){
            return;
        }
        let content = fs.readFile(path.join('.',file),'UTF-8');
        content.then(function(data){
            console.log(file)
            //console.log(data);
        })
    });
})
//阻塞进程 放置退出
process.stdin.resume();

 

const fs = require('fs').promises;const path = require('path');const EventEmitter = require('events');
let emitter = new EventEmitter();
async function walk(dir) {    let files = await fs.readdir(dir);    files = await Promise.all(files.map(async file => {        const filePath = path.join(dir, file);        const stats = await fs.stat(filePath);        if (stats.isDirectory()) return walk(filePath);        else if(stats.isFile()) return filePath;    }));
    return files.reduce((all, folderContents) => all.concat(folderContents), []);}var walker=walk('.');
walker.then(function(files){files.map(function(file){if(file.indexOf('.vmCmp')<0){return;}let content = fs.readFile(path.join('.',file),'UTF-8');content.then(function(data){console.log(file)//console.log(data);})});})
process.stdin.resume();

posted @ 2019-11-16 18:07  wolbo  阅读(1018)  评论(0编辑  收藏  举报