gpt4 book ai didi

node.js - 创建一个包含文件夹内容的数组

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:32 24 4
gpt4 key购买 nike

我有一个包含一些文本文档的文件夹,

我想创建一个包含文本文档名称的数组。

结果一定是这样的:

数组=[第一个文件.txt],[第二个文件.txt],[第三个文件.txt],ecc。

我知道我必须做这样的事情:

fs.readFile(foldername, function (err, content) {

var array = content.toString().split("...");
}

但我不知道应该在 .split() 中写什么。

有什么想法吗?谢谢。

最佳答案

您可能应该使用fs.readdir而不是fs.readFile

fs.readFile 用于获取特定文件的内容,传递给它的路径参数应该是特定文件的路径,而不是文件夹的路径。

fs.readdir 用于获取特定文件夹/目录的内容。这里的路径参数应该是文件夹的路径

Node 文档说:

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 '..'.

Read more here 。您必须查看文件名是否包含您想要的文件扩展名。

关于node.js - 创建一个包含文件夹内容的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com