Node.js 使用递归实现遍历文件夹中所有文件

如标题所示,遍历文件夹下的所有文件,主要功能如下:

传入一个路径,读取路径里面所有的文件
遍历读取的文件,判断当前文件是文件还是文件夹
当前目录为文件,打印出当前文件绝对路径
当前目录为文件夹,获取文件夹路径,继续读取路径下文件
遍历完目录中的所有文件为止
代码中用到的几个方法

path.resolve(path)

一个路径或路径片段解析成一个绝对路径,返回解析后的路径字符串
fs.readdir(path[,option],callback)

读取目录下面的文件,返回目录下的文件列表对象,如果传入的是个文件,返回这个文件

fs.stat(path,callback)

获取文件信息对象Stats,包括文件大小,gid等信息

stats.isFile()

文件信息对象Stats的一个方法,判断当前文件是不是一个文件

stats.isDirectory()

文件信息对象Stats的一个方法,判断当前文件是不是一个文件夹

代码和注释如下:

var fs = require('fs');
var path = require('path');

//解析需要遍历的文件夹,我这以E盘根目录为例
var filePath = path.resolve('E:');

//调用文件遍历方法
fileDisplay(filePath);

/**
 * 文件遍历方法
 * @param filePath 需要遍历的文件路径
 */
function fileDisplay(filePath){
  //根据文件路径读取文件,返回文件列表
  fs.readdir(filePath,function(err,files){
    if(err){
      console.warn(err)
    }else{
      //遍历读取到的文件列表
      files.forEach(function(filename){
        //获取当前文件的绝对路径
        var filedir = path.join(filePath,filename);
        //根据文件路径获取文件信息,返回一个fs.Stats对象
        fs.stat(filedir,function(eror,stats){
          if(eror){
            console.warn('获取文件stats失败');
          }else{
            var isFile = stats.isFile();//是文件
            var isDir = stats.isDirectory();//是文件夹
            if(isFile){
              console.log(filedir);
            }
            if(isDir){
              fileDisplay(filedir);//递归,如果是文件夹,就继续遍历该文件夹下面的文件
            }
          }
        })
      });
    }
  });
}

运行结果为:

E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\AbstractCacheInvoker.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\AbstractCacheResolver.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\BasicOperation.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheableOperation.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\BeanFactoryCacheOperationSourceAdvisor.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\AbstractFallbackCacheOperationSource.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheAspectSupport.CacheOperationContext.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheAspectSupport.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheAspectSupport.CacheOperationMetadata.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheErrorHandler.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheEvictOperation.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheInterceptor.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheOperation.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheOperationInvocationContext.html
E:\jars\spring-framework-4.2.9.RELEASE\docs\javadoc-api\org\springframework\cache\interceptor\CacheOperationInvoker.html
············