本文详解如何利用 Node.js 的 fs 和 path 模块,通过递归算法遍历指定目录下的所有文件。文章解析了 path.resolve、fs.readdir、stats.isFile 及 stats.isDirectory 等关键 API 的用法,并提供完整的遍历代码示例与运行结果分析,帮助开发者掌握文件夹内容读取的核心技术。
Node.js 递归遍历文件夹:核心方法与完整代码解析
本文旨在介绍如何遍历文件夹下的所有文件,主要实现以下功能。
首先传入一个目标路径,读取该路径下的所有文件;接着遍历读取到的文件列表,判断当前项是普通文件还是文件夹;若当前目录为文件,则打印其绝对路径;若当前目录为文件夹,则获取该文件夹路径并继续读取其下的文件;此过程持续进行,直到遍历完目录中的所有文件为止。
path.resolve(path)
代码实现中涉及以下几个关键方法:将路径或路径片段解析为绝对路径,并返回解析后的路径字符串;以及 fs.readdir(path[,option],callback),用于读取目录内容。
该 API 用于读取目录下面的文件,返回包含目录下文件列表的对象;如果传入的是单个文件路径,则返回该文件信息。
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
············
至此,Node.js 遍历文件夹的实现方法介绍完毕,希望本文能对大家有所帮助。