之前用来做批量 重命名的一个 js,
const fs = require("fs");
const path = require("path");
// 要被替换的 字符串
const pattern = '日期'
const ignoreDirs = [
'.TemporaryItems',
'.Spotlight-V100',
'.DocumentRevisions-V100',
'.000',
'.DS_Store',
'.Trashes'
]
// 目标字符串 默认为空字符串
const targetStr = "";
const executeRename = async (parentDir, strPattern, targetStr) => {
// const currentPath = path.resolve(parentDir)
// console.log('-----currentPath: ', currentPath);
const dirs = fs.readdirSync(parentDir);
// console.log('---dirs:', dirs);
for (dir of dirs) {
let currentPath = path.resolve(parentDir, dir);
if (ignoreDirs.includes(currentPath)) {
continue;
}
if (ignoreDirs.includes(dir)) {
continue;
}
if (currentPath.indexOf(strPattern)) {
// edge case
// if (/^\/\d*\.?/g.test(currentPath)) {
// return;
// }
console.log("*******************************************");
console.log("-------currentPath: ", currentPath);
console.log("-------strPattern: ", strPattern);
console.log("-------targetStr: ", targetStr);
const newPath = currentPath.replace(strPattern, targetStr);
console.log("-------nextPath: ", currentPath);
console.log("-------newPath: ", newPath);
fs.renameSync(currentPath, newPath);
currentPath = newPath;
}
const stat = await fs.lstatSync(currentPath);
if (stat.isDirectory()) {
await executeRename(currentPath, strPattern, targetStr);
}
}
};
executeRename(".", pattern, targetStr);