{"title":"写一个获取文件夹内容的JS代码","description":"以下是一个获取文件夹内容的JS代码示例:\n\njavascript\nconst fs = require('fs');\n\nfunction getFolderContent(folderPath) {\n try {\n const files = fs.readdirSync(folderPath);\n return files;\n } catch (error) {\n console.error('Error reading folder content:', error);\n return [];\n }\n}\n\n// 用法示例\nconst folderPath = './path/to/folder';\nconst folderContent = getFolderContent(folderPath);\nconsole.log('Folder content:', folderContent);\n\n\n这段代码使用Node.js的fs模块来读取文件夹的内容。getFolderContent函数接受一个文件夹路径作为参数,并使用fs.readdirSync方法同步地读取文件夹内容。如果读取文件夹内容出现错误,函数会打印错误信息并返回一个空数组。\n\n使用示例中,可以将要读取的文件夹路径传递给getFolderContent函数,然后获取文件夹内容并打印到控制台。请注意替换./path/to/folder为实际的文件夹路径。","keywords":"Node.js, JavaScript, 文件夹, 读取, 文件, fs, readdirSync, 代码示例, 获取内容"}

Node.js 获取文件夹内容的代码示例 - JavaScript 读取文件夹

原文地址: https://www.cveoy.top/t/topic/pZIo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录