1. 程式人生 > >Node.js使用遞迴實現遍歷資料夾中所有檔案

Node.js使用遞迴實現遍歷資料夾中所有檔案

https://blog.csdn.net/younglao/article/details/77046830?locationNum=8&fps=1

版權宣告:本文為博主原創文章,未經博主允許不得轉載。    https://blog.csdn.net/younglao/article/details/77046830
如標題所示,遍歷資料夾下的所有檔案,主要功能如下:
傳入一個路徑,讀取路徑裡面所有的檔案
遍歷讀取的檔案,判斷當前檔案是檔案還是資料夾
當前目錄為檔案,打印出當前檔案絕對路徑
當前目錄為資料夾,獲取資料夾路徑,繼續讀取路徑下檔案
遍歷完目錄中的所有檔案為止
程式碼中用到的幾個方法

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);//遞迴,如果是資料夾,就繼續遍歷該資料夾下面的檔案
                        }
                    }
                })
            });
        }
    });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
執行結果為:
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
············
--------------------- 
作者:younglao 
來源:CSDN 
原文:https://blog.csdn.net/younglao/article/details/77046830 
版權宣告:本文為博主原創文章,轉載請附上博文連結!