How do I get a list of files with specific file extension using node.js?

后端 未结 4 1354
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 00:37

The node fs package has the following methods to list a directory:

fs.readdir(path, [callback]) Asynchronous readdir(3). Reads the

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 01:13

    I used the following code and its working fine:

    var fs = require('fs');
    var path = require('path');
    var dirPath = path.resolve(__dirname); // path to your directory goes here
    var filesList;
    fs.readdir(dirPath, function(err, files){
      filesList = files.filter(function(e){
        return path.extname(e).toLowerCase() === '.txt'
      });
      console.log(filesList);
    });
    

提交回复
热议问题