获取某个文件夹下的所有信息
引用自:https://blog.csdn.net/m0_62323931/article/details/132532830const fs = require('fs'); const path = require('path'); function getAllFilesInfo(dirPath) { const itemsInfo = []; function traverseDirectory(currentPath) { const items = fs.readdirSync(currentPath); for (const item of items) { const itemPath = path.join(currentPath, item); const stat = fs.statSync(itemPath); if (stat.isFile() || stat.isDirectory()) { itemsInfo.push({ name: item, path: itemPath, size: stat.size, createdAt: stat.ctime, modifiedAt: stat.mtime, isDirectory: stat.isDirectory() }); } if (stat.isDirectory()) { traverseDirectory(itemPath); } } } traverseDirectory(dirPath); return itemsInfo; } const folderPath = './files/大名'; const itemsInfo = getAllFilesInfo(folderPath); console.log(itemsInfo); //输出 [ { name: '新建 DOCX 文档.docx', path: 'files\\大名\\新建 DOCX 文档.docx', size: 0, createdAt: 2023-08-15T04:33:51.945Z, modifiedAt: 2023-08-15T04:33:51.397Z, isDirectory: false }, { name: '新建 XLSX 工作表.xlsx', path: 'files\\大名\\新建 XLSX 工作表.xlsx', size: 8861, createdAt: 2023-08-15T04:33:57.256Z, modifiedAt: 2023-08-15T04:33:57.251Z, isDirectory: false }, { name: '新建 文本文档.txt', path: 'files\\大名\\新建 文本文档.txt', size: 1083, createdAt: 2023-08-15T04:33:47.844Z, modifiedAt: 2023-08-10T07:48:51.190Z, isDirectory: false }, { name: '第二层', path: 'files\\大名\\第二层', size: 0, createdAt: 2023-08-15T04:46:03.472Z, modifiedAt: 2023-08-15T04:46:03.472Z, isDirectory: true }, { name: '新建 DOCX 文档.docx', path: 'files\\大名\\第二层\\新建 DOCX 文档.docx', size: 0, createdAt: 2023-08-15T04:33:51.945Z, modifiedAt: 2023-08-15T04:33:51.397Z, isDirectory: false }, { name: '新建 XLSX 工作表.xlsx', path: 'files\\大名\\第二层\\新建 XLSX 工作表.xlsx', size: 8861, createdAt: 2023-08-15T04:33:57.256Z, modifiedAt: 2023-08-15T04:33:57.251Z, isDirectory: false }, { name: '新建 文本文档.txt', path: 'files\\大名\\第二层\\新建 文本文档.txt', size: 1083, createdAt: 2023-08-15T04:33:47.844Z, modifiedAt: 2023-08-10T07:48:51.190Z, isDirectory: false }, { name: '第三层', path: 'files\\大名\\第二层\\第三层', size: 0, createdAt: 2023-08-15T04:46:03.471Z, modifiedAt: 2023-08-15T04:46:03.471Z, isDirectory: true }, { name: '新建 DOCX 文档.docx', path: 'files\\大名\\第二层\\第三层\\新建 DOCX 文档.docx', size: 0, createdAt: 2023-08-15T04:33:51.945Z, modifiedAt: 2023-08-15T04:33:51.397Z, isDirectory: false }, { name: '新建 XLSX 工作表.xlsx', path: 'files\\大名\\第二层\\第三层\\新建 XLSX 工作表.xlsx', size: 8861, createdAt: 2023-08-15T04:33:57.256Z, modifiedAt: 2023-08-15T04:33:57.251Z, isDirectory: false }, { name: '新建 文本文档.txt', path: 'files\\大名\\第二层\\第三层\\新建 文本文档.txt', size: 1083, createdAt: 2023-08-15T04:33:47.844Z, modifiedAt: 2023-08-10T07:48:51.190Z, isDirectory: false }, { name: '第三层2', path: 'files\\大名\\第二层\\第三层2', size: 0, createdAt: 2023-08-15T04:46:03.475Z, modifiedAt: 2023-08-15T04:46:03.475Z, isDirectory: true }, { name: '新建 DOCX 文档.docx', path: 'files\\大名\\第二层\\第三层2\\新建 DOCX 文档.docx', size: 0, createdAt: 2023-08-15T04:33:51.945Z, modifiedAt: 2023-08-15T04:33:51.397Z, isDirectory: false }, { name: '新建 XLSX 工作表.xlsx', path: 'files\\大名\\第二层\\第三层2\\新建 XLSX 工作表.xlsx', size: 8861, createdAt: 2023-08-15T04:33:57.256Z, modifiedAt: 2023-08-15T04:33:57.251Z, isDirectory: false }, { name: '新建 文本文档.txt', path: 'files\\大名\\第二层\\第三层2\\新建 文本文档.txt', size: 1083, createdAt: 2023-08-15T04:33:47.844Z, modifiedAt: 2023-08-10T07:48:51.190Z, isDirectory: false }, { name: '末尾', path: 'files\\大名\\第二层\\第三层2\\末尾', size: 0, createdAt: 2023-08-15T04:46:03.478Z, modifiedAt: 2023-08-15T04:46:03.478Z, isDirectory: true }, { name: '新建 DOCX 文档.docx', path: 'files\\大名\\第二层\\第三层2\\末尾\\新建 DOCX 文档.docx', size: 0, createdAt: 2023-08-15T04:33:51.945Z, modifiedAt: 2023-08-15T04:33:51.397Z, isDirectory: false }, { name: '新建 XLSX 工作表.xlsx', path: 'files\\大名\\第二层\\第三层2\\末尾\\新建 XLSX 工作表.xlsx', size: 8861, createdAt: 2023-08-15T04:33:57.256Z, modifiedAt: 2023-08-15T04:33:57.251Z, isDirectory: false }, { name: '新建 文本文档.txt', path: 'files\\大名\\第二层\\第三层2\\末尾\\新建 文本文档.txt', size: 1083, createdAt: 2023-08-15T04:33:47.844Z, modifiedAt: 2023-08-10T07:48:51.190Z, isDirectory: false }, { name: '空', path: 'files\\大名\\第二层\\第三层2\\末尾\\空', size: 0, createdAt: 2023-08-15T04:46:03.477Z, modifiedAt: 2023-08-15T04:46:03.477Z, isDirectory: true } ]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。