zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    计算小于12的阶乘
    ubuntu下gvim启动出现gtk warning Invalid input string
    UBUNTU基础知识
    Ubuntu下创建软链接
    linux命令行介绍及使用(二)
    Ubuntu问题sudo: /etc/sudoers is mode 0640should be 0440的解决方法
    安装mp3插件
    Ubuntu下GTK的安装
    linux命令行介绍及使用(三)
    VB.NET中用GDI+画饼图
  • 原文地址:https://www.cnblogs.com/wolbo/p/11872788.html
Copyright © 2011-2022 走看看