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

后端 未结 4 1340
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 01:11

    Basically, you do something like this:

    const path = require('path')
    const fs = require('fs')
    
    const dirpath = path.join(__dirname, '/path')
    
    fs.readdir(dirpath, function(err, files) {
      const txtFiles = files.filter(el => /\.txt$/.test(el))
      // do something with your files, by the way they are just filenames...
    })
    

提交回复
热议问题