如何使用node.js获取具有特定文件扩展名的文件列表?

时间:2022-08-25 23:23:21

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

节点fs包具有以下列出目录的方法:

fs.readdir(path, [callback]) Asynchronous readdir(3). Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.

fs.readdir(path,[callback])异步readdir(3)。读取目录的内容。回调有两个参数(错误,文件),其中files是目录中文件名的数组,不包括'。'和'..'。

fs.readdirSync(path) Synchronous readdir(3). Returns an array of filenames excluding '.' and '..

fs.readdirSync(path)同步readdir(3)。返回不包含'。'的文件名数组和'..

But how do I get a list of files matching a file specification, for example *.txt?

但是如何获得与文件规范匹配的文件列表,例如* .txt?

4 个解决方案

#1


12  

You could filter they array of files with an extension extractor function. The path module provides one such function, if you don't want to write your own string manipulation logic or regex.

您可以使用扩展提取器功能过滤它们的文件数组。如果您不想编写自己的字符串操作逻辑或正则表达式,则路径模块提供一个此类函数。

var path = require('path');

var EXTENSION = '.txt';

var targetFiles = files.filter(function(file) {
    return path.extname(file).toLowerCase() === EXTENSION;
});

EDIT As per @arboreal84's suggestion, you may want to consider cases such as myfile.TXT, not too uncommon. I just tested it myself and path.extname does not do lowercasing for you.

编辑根据@ arboreal84的建议,你可能想要考虑诸如myfile.TXT之类的情况,这并不太常见。我自己测试了它,而path.extname不会为你做小写。

#2


5  

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...
})

#3


2  

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);
});

#4


1  

fs doesn't support filtering itself but if you don't want to filter youself then use glob

fs不支持过滤本身,但如果你不想过滤自己,那么使用glob

var glob = require('glob');

// options is optional
glob("**/*.js", options, function (er, files) {
  // files is an array of filenames.
  // If the `nonull` option is set, and nothing
  // was found, then files is ["**/*.js"]
  // er is an error object or null.
})

#1


12  

You could filter they array of files with an extension extractor function. The path module provides one such function, if you don't want to write your own string manipulation logic or regex.

您可以使用扩展提取器功能过滤它们的文件数组。如果您不想编写自己的字符串操作逻辑或正则表达式,则路径模块提供一个此类函数。

var path = require('path');

var EXTENSION = '.txt';

var targetFiles = files.filter(function(file) {
    return path.extname(file).toLowerCase() === EXTENSION;
});

EDIT As per @arboreal84's suggestion, you may want to consider cases such as myfile.TXT, not too uncommon. I just tested it myself and path.extname does not do lowercasing for you.

编辑根据@ arboreal84的建议,你可能想要考虑诸如myfile.TXT之类的情况,这并不太常见。我自己测试了它,而path.extname不会为你做小写。

#2


5  

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...
})

#3


2  

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);
});

#4


1  

fs doesn't support filtering itself but if you don't want to filter youself then use glob

fs不支持过滤本身,但如果你不想过滤自己,那么使用glob

var glob = require('glob');

// options is optional
glob("**/*.js", options, function (er, files) {
  // files is an array of filenames.
  // If the `nonull` option is set, and nothing
  // was found, then files is ["**/*.js"]
  // er is an error object or null.
})