如標題所示,遍歷文件夾下的所有文件,主要功能如下:
傳入一個路徑,讀取路徑里面所有的文件
遍歷讀取的文件,判斷當前文件是文件還是文件夾
當前目錄為文件,打印出當前文件絕對路徑
當前目錄為文件夾,獲取文件夾路徑,繼續(xù)讀取路徑下文件
遍歷完目錄中的所有文件為止
代碼中用到的幾個方法
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:'); //調(diào)用文件遍歷方法 fileDisplay(filePath); /** * 文件遍歷方法 * @param filePath 需要遍歷的文件路徑 */ function fileDisplay(filePath){ //根據(jù)文件路徑讀取文件,返回文件列表 fs.readdir(filePath,function(err,files){ if(err){ console.warn(err) }else{ //遍歷讀取到的文件列表 files.forEach(function(filename){ //獲取當前文件的絕對路徑 var filedir = path.join(filePath,filename); //根據(jù)文件路徑獲取文件信息,返回一個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);//遞歸,如果是文件夾,就繼續(xù)遍歷該文件夾下面的文件 } } }) }); } }); }
運行結(jié)果為:
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 遍歷文件夾的實現(xiàn)方法就結(jié)束了,希望大家以后多多支持腳本之家。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com