|
|
|
|
@ -100,24 +100,57 @@ ipcMain.handle('watch-upload-analysis-file', async (e, id) => { |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 递归获取所有文件(包括子文件夹中的文件)
|
|
|
|
|
function getAllFilesRecursively(dir, fileList = []) { |
|
|
|
|
console.log(`扫描目录: ${dir}`); |
|
|
|
|
try { |
|
|
|
|
const files = fs.readdirSync(dir); |
|
|
|
|
console.log(`找到 ${files.length} 个项目:`, files); |
|
|
|
|
|
|
|
|
|
files.forEach(file => { |
|
|
|
|
const fullPath = path.join(dir, file); |
|
|
|
|
try { |
|
|
|
|
const stat = fs.statSync(fullPath); |
|
|
|
|
if (stat.isDirectory()) { |
|
|
|
|
// 如果是文件夹,递归查找
|
|
|
|
|
console.log(` 发现子文件夹: ${file}`); |
|
|
|
|
getAllFilesRecursively(fullPath, fileList); |
|
|
|
|
} else if (stat.isFile()) { |
|
|
|
|
// 如果是文件,添加到列表
|
|
|
|
|
console.log(` 发现文件: ${file}, 大小: ${stat.size} bytes, 修改时间: ${stat.mtime}`); |
|
|
|
|
fileList.push({ |
|
|
|
|
file: file, |
|
|
|
|
fullPath: fullPath, |
|
|
|
|
stat: stat |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} catch (err) { |
|
|
|
|
console.warn(`无法访问文件: ${fullPath}`, err.message); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} catch (err) { |
|
|
|
|
console.error(`读取目录失败 ${dir}:`, err.message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fileList; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 注册IPC方法:读取C盘指定文件夹的文件列表
|
|
|
|
|
ipcMain.handle('read-c-folder', async (event, folderPath) => { |
|
|
|
|
try { |
|
|
|
|
let dataJson = loadConfig() |
|
|
|
|
const { path: targetPath, orderNum } = folderPath; |
|
|
|
|
|
|
|
|
|
console.log('开始扫描目录:', targetPath); |
|
|
|
|
|
|
|
|
|
// const targetPath = folderPath;
|
|
|
|
|
if (!fs.existsSync(targetPath)) { |
|
|
|
|
return { success: false, msg: `文件夹不存在:${targetPath}` }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 读取所有文件(过滤掉目录)
|
|
|
|
|
const files = fs.readdirSync(targetPath).map(file => { |
|
|
|
|
const fullPath = path.join(targetPath, file); |
|
|
|
|
const stat = fs.statSync(fullPath); |
|
|
|
|
return { file, fullPath, stat }; |
|
|
|
|
}) |
|
|
|
|
.filter(item => item.stat.isFile()); |
|
|
|
|
// 递归读取所有文件(包括子文件夹中的文件)
|
|
|
|
|
const files = getAllFilesRecursively(targetPath); |
|
|
|
|
console.log(`总共找到 ${files.length} 个文件`); |
|
|
|
|
|
|
|
|
|
if (files.length === 0) { |
|
|
|
|
return { success: false, msg: '文件夹中没有文件' }; |
|
|
|
|
@ -128,14 +161,16 @@ ipcMain.handle('read-c-folder', async (event, folderPath) => { |
|
|
|
|
return current.stat.mtimeMs > prev.stat.mtimeMs ? current : prev; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
console.log(`最新文件: ${latestFile.file}, 路径: ${latestFile.fullPath}, 修改时间: ${latestFile.stat.mtime}`); |
|
|
|
|
|
|
|
|
|
const { file: oldName, fullPath: oldPath, stat } = latestFile; |
|
|
|
|
console.log('latestFile', latestFile); |
|
|
|
|
// 获取文件所在目录(可能是子文件夹)
|
|
|
|
|
const fileDir = path.dirname(oldPath); |
|
|
|
|
// 如果未提供 orderNum
|
|
|
|
|
const ext = path.extname(oldName); // 保留原扩展名,如 .txt
|
|
|
|
|
const baseNewName = orderNum; |
|
|
|
|
const newName = baseNewName + (ext.startsWith('.') ? ext : ''); |
|
|
|
|
const newPath = path.join(targetPath, newName); |
|
|
|
|
const newPath = path.join(fileDir, newName); |
|
|
|
|
console.log('newPath', newPath, dataJson.device.updateFileName == '1'); |
|
|
|
|
if (dataJson.device.updateFileName == '1') { |
|
|
|
|
fs.renameSync(oldPath, newPath); |
|
|
|
|
@ -156,10 +191,7 @@ ipcMain.handle('read-c-folder', async (event, folderPath) => { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
fs.renameSync(oldPath, oldPath); |
|
|
|
|
// 执行重命名
|
|
|
|
|
// 可选:读取文件 Buffer(注意大文件内存问题)
|
|
|
|
|
// 如果不需要内容,可移除 fileBuffer 字段
|
|
|
|
|
// 不重命名,直接使用原文件
|
|
|
|
|
const fileBuffer = fs.readFileSync(oldPath); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
@ -175,6 +207,7 @@ ipcMain.handle('read-c-folder', async (event, folderPath) => { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
console.error('read-c-folder 错误:', err); |
|
|
|
|
return { success: false, msg: `读取最新文件失败:${err.message}` }; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
@ -286,17 +319,15 @@ ipcMain.handle('rename-file', async (event, data) => { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
fs.renameSync(oldPath, oldPath); |
|
|
|
|
// 执行重命名
|
|
|
|
|
// 可选:读取文件 Buffer(注意大文件内存问题)
|
|
|
|
|
// 如果不需要内容,可移除 fileBuffer 字段
|
|
|
|
|
const fileBuffer = fs.readFileSync(oldPath); |
|
|
|
|
// 不重命名,直接使用原文件
|
|
|
|
|
const oldName = path.basename(filePath); |
|
|
|
|
const fileBuffer = fs.readFileSync(filePath); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
success: true, |
|
|
|
|
data: { |
|
|
|
|
fileName: oldName, |
|
|
|
|
filePath: oldPath, |
|
|
|
|
filePath: filePath, |
|
|
|
|
fileSize: (stat.size / 1024).toFixed(2) + 'KB', |
|
|
|
|
updateTime: new Date(stat.mtime).toLocaleString(), |
|
|
|
|
fileBuffer: fileBuffer // 若不需要,可删除此行
|
|
|
|
|
|