javascript 如何在nodejs的目录中获取第一个扩展名为.txt的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17518193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the first file with a .txt extension in a directory with nodejs?
提问by Rolando
The directory all my files are in is: '/usr/home/jordan' and I have many files under there (in the directory itself, but one file that is named with a .txt extension.
我的所有文件所在的目录是:'/usr/home/jordan',我在那里有很多文件(在目录本身中,但有一个以 .txt 扩展名命名的文件。
With nodejs and fs, I want to put that first file (or any file) with a txt extension into "mytxtfilepath". I only have a single .txt file in the whole directory (amongst many other files but with different extensions) The single .txt file could be named ANYTHING, I cannot guarantee what the name will be at any given time, only that it ends in .txt:
使用 nodejs 和 fs,我想将带有 txt 扩展名的第一个文件(或任何文件)放入“mytxtfilepath”。我在整个目录中只有一个 .txt 文件(在许多其他文件中,但具有不同的扩展名)单个 .txt 文件可以命名为任何东西,我不能保证在任何给定时间的名称是什么,只是它以。文本文件:
var homedir = "/usr/home/jordan";
var mytxtfilepath=homedir + "???????";
fs.readfile(mytxtfilepath, function(err,data) {
console.log(data);
});
How do I put the correct path to my txt file without hardcoding the name of the txt file itself?
如何在不硬编码 txt 文件本身的名称的情况下将正确的路径放入我的 txt 文件?
回答by everconfusedGuy
var files = fs.readdirSync(homedir);
var path = require('path');
for(var i in files) {
if(path.extname(files[i]) === ".txt") {
//do something
}
}
回答by victommasi
You can use Glob Moduleas well. It works fine for me!
您也可以使用Glob 模块。这对我来说可以!
var glob = require( 'glob' );
var myPath= "/fileFolder/**/*.txt";
glob(myPath, function (er, files) {
// Files is an array of filenames.
// Do something with files.
})
回答by polkovnikov.ph
The lazy solution:
懒人解决办法:
npm install --save readdir
and then
接着
const {read} = require('readdir');
read("/usr/home/jordan", "**/*.txt", (err, paths) =>
console.log(paths)
);
回答by go-oleg
You can use fs.readdirto list the files and find the one that ends with .txt
:
您可以使用fs.readdir列出文件并找到以 结尾的文件.txt
:
var myPath = "/usr/home/jordan";
fs.readdir(path, function(fileNames) {
for(var i = 0; i < fileNames.length; i++) {
var fileName = fileNames[i];
if(path.extname(fileName) === ".txt") {
fs.readfile(path.join(myPath,fileName), function(err,data) {
console.log(data);
});
break;
}
}
}
);
Note this requires pathso add var path = require("path")
at the top.
请注意,这需要路径,因此var path = require("path")
在顶部添加。
回答by mquandalle
You can use fs.readdirand path.extname
您可以使用fs.readdir和path.extname
var fs = require('fs')
, path = require('path');
function getFileWithExtensionName(dir, ext) {
fs.readdir(dir, function(files){
for (var i = 0; i < files.length; i++) {
if (path.extname(files[i]) === '.' + ext)
return files[i]
}
});
}
var homedir = "/usr/home/jordan";
var mytxtfilepath= getFileWithExtensionName(homedir, 'txt')
fs.readfile(mytxtfilepath, function(err,data) {
console.log(data);
});