如標題所示,遍歷文件夾下的所有文件,主要功能如下:
傳入一個路徑,讀取路徑里面所有的文件
遍歷讀取的文件,判斷當前文件是文件還是文件夾
當前目錄為文件,打印出當前文件絕對路徑
當前目錄為文件夾,獲取文件夾路徑,繼續讀取路徑下文件
遍歷完目錄中的所有文件為止
代碼中用到的幾個方法
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.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/AbstractCacheResolver.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/BasicOperation.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheableOperation.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/BeanFactoryCacheOperationSourceAdvisor.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheAspectSupport.CacheOperationContext.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheAspectSupport.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheAspectSupport.CacheOperationMetadata.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheErrorHandler.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheEvictOperation.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheInterceptor.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheOperation.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheOperationInvocationContext.htmlE:/jars/spring-framework-4.2.9.RELEASE/docs/javadoc-api/org/springframework/cache/interceptor/CacheOperationInvoker.html????????????
到這Node.js 遍歷文件夾的實現方法就結束了,希望大家以后多多支持武林網。
新聞熱點
疑難解答