在node.js中,我可以列出具有匹配的通配符的文件
fs.readdirSync('C:/tmp/*.csv')?
Run Code Online (Sandbox Code Playgroud)
我没有从fs文档中找到有关外卡匹配的信息.
Mor*_*len 64
Node核心不包含这一点.您可以查看此模块,了解您的目标.npmjs.org也是查找各种模块的绝佳资源.
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.
})
Run Code Online (Sandbox Code Playgroud)
Jai*_*ime 53
如果您不想为项目添加新的依赖项(例如glob),则可以使用普通的js/node函数,例如:
var files = fs.readdirSync('C:/tmp').filter(fn => fn.endsWith('.csv'));
Run Code Online (Sandbox Code Playgroud)
Regex 可能有助于更复杂的比较
如果glob不是你想要的,或者有点令人困惑,那么还有glob-fs.该文档涵盖了许多使用场景和示例.
// sync
var files = glob.readdirSync('*.js', {});
// async
glob.readdir('*.js', function(err, files) {
console.log(files);
});
// stream
glob.readdirStream('*.js', {})
.on('data', function(file) {
console.log(file);
});
// promise
glob.readdirPromise('*.js')
.then(function(files) {
console.log(file);
});
Run Code Online (Sandbox Code Playgroud)
小智 7
开箱即用,非常简单
import fs from 'fs'
fs.readdirSync('C:/tmp/').filter((allFilesPaths:string) =>
allFilesPaths.match(/\.csv$/) !== null)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46773 次 |
| 最近记录: |