const 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
}
]
输出数据按目录层级显示
const fs = require('fs');
const path = require('path');
function getAllFilesInfo(dirPath) {
function traverseDirectory(currentPath) {
const items = fs.readdirSync(currentPath);
const itemsInfo = [];
for (const item of items) {
const itemPath = path.join(currentPath, item);
const stat = fs.statSync(itemPath);
const itemInfo = {
name: item,
path: itemPath,
size: stat.size,
createdAt: stat.ctime,
modifiedAt: stat.mtime,
isDirectory: stat.isDirectory(),
children: []
};
if (itemInfo.isDirectory) {
itemInfo.children = traverseDirectory(itemPath);
}
itemsInfo.push(itemInfo);
}
return itemsInfo;
}
return traverseDirectory(dirPath);
}
const folderPath = './files/大名';
const itemsInfo = getAllFilesInfo(folderPath);
console.log(JSON.stringify(itemsInfo, null, 2)); // Output with 2-space indentation
//输出
[
{
"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,
"children": []
},
{
"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,
"children": []
},
{
"name": "新建 文本文档.txt",
"path": "files\\大名\\新建 文本文档.txt",
"size": 1083,
"createdAt": "2023-08-15T04:33:47.844Z",
"modifiedAt": "2023-08-10T07:48:51.190Z",
"isDirectory": false,
"children": []
},
{
"name": "第二层",
"path": "files\\大名\\第二层",
"size": 0,
"createdAt": "2023-08-15T04:46:03.472Z",
"modifiedAt": "2023-08-15T04:46:03.472Z",
"isDirectory": true,
"children": [
{
"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,
"children": []
},
{
"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,
"children": []
},
{
"name": "新建 文本文档.txt",
"path": "files\\大名\\第二层\\新建 文本文档.txt",
"size": 1083,
"createdAt": "2023-08-15T04:33:47.844Z",
"modifiedAt": "2023-08-10T07:48:51.190Z",
"isDirectory": false,
"children": []
},
{
"name": "第三层",
"path": "files\\大名\\第二层\\第三层",
"size": 0,
"createdAt": "2023-08-15T04:46:03.471Z",
"modifiedAt": "2023-08-15T04:46:03.471Z",
"isDirectory": true,
"children": [
{
"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,
"children": []
},
{
"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,
"children": []
},
{
"name": "新建 文本文档.txt",
"path": "files\\大名\\第二层\\第三层\\新建 文本文档.txt",
"size": 1083,
"createdAt": "2023-08-15T04:33:47.844Z",
"modifiedAt": "2023-08-10T07:48:51.190Z",
"isDirectory": false,
"children": []
}
]
},
{
"name": "第三层2",
"path": "files\\大名\\第二层\\第三层2",
"size": 0,
"createdAt": "2023-08-15T04:46:03.475Z",
"modifiedAt": "2023-08-15T04:46:03.475Z",
"isDirectory": true,
"children": [
{
"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,
"children": []
},
{
"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,
"children": []
},
{
"name": "新建 文本文档.txt",
"path": "files\\大名\\第二层\\第三层2\\新建 文本文档.txt",
"size": 1083,
"createdAt": "2023-08-15T04:33:47.844Z",
"modifiedAt": "2023-08-10T07:48:51.190Z",
"isDirectory": false,
"children": []
},
{
"name": "末尾",
"path": "files\\大名\\第二层\\第三层2\\末尾",
"size": 0,
"createdAt": "2023-08-15T04:46:03.478Z",
"modifiedAt": "2023-08-15T04:46:03.478Z",
"isDirectory": true,
"children": [
{
"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,
"children": []
},
{
"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,
"children": []
},
{
"name": "新建 文本文档.txt",
"path": "files\\大名\\第二层\\第三层2\\末尾\\新建 文本文档.txt",
"size": 1083,
"createdAt": "2023-08-15T04:33:47.844Z",
"modifiedAt": "2023-08-10T07:48:51.190Z",
"isDirectory": false,
"children": []
},
{
"name": "空",
"path": "files\\大名\\第二层\\第三层2\\末尾\\空",
"size": 0,
"createdAt": "2023-08-15T04:46:03.477Z",
"modifiedAt": "2023-08-15T04:46:03.477Z",
"isDirectory": true,
"children": []
}
]
}
]
}
]
}
]