javascript - 遍历文件夹中的文件(javascript)
问题描述
我正在寻找遍历文件夹中的文件。我不知道运行脚本时文件的名称是什么,我只想获取名称。
我希望它像这样运行,但我不知道如何。我明白为什么这不起作用,但我不明白如何让它按我想要的方式工作。
for (file in "./levels/") {
console.log(file)
}
有没有办法让它像这样运行?
解决方案
const fs = require("fs");
fs.readdirSync(".levels/").forEach(file => {
//Print file name
console.log(file)
/*
Run this to print the file contents
console.log(readFileSync(".levels/" + file, {encoding: "utf8"}))
*/
})
//but if your goal is just to print the file name you can do this
fs.readFileSync(".levels/").forEach(console.log)
推荐阅读
- android - 与正确显示文本的虚拟设备相比,为什么我的文本在 Google 商店中消失了?
- python - 使用python解析getEmailActivityUserDetail报告
- spring - 如何跟踪 WebClient POST/GET 请求的进度 - Spring Boot?
- reactjs - Is there a way to force tab unmount in ChakraUI tabs component?
- jira - 在现有 Confluence 应用程序中更改指向 Jira OAuth 的链接
- node.js - 使用“纱线”运行命令但没有它的情况下出现纱线问题
- c++ - 为什么我的程序从 csv 文件 c++ 输出特殊字符
- php - 需要凭据 php twilio
- processing - 如何使这种模式扩大和缩小
- python - Python Pandas:从表中仅获取 3 个第一个元素