好友
阅读权限10
听众
最后登录1970-1-1
|
const { readdirSync, readFileSync, renameSync } = require('node:fs');
const { join } = require('node:path');
const RUNTIME_CWD = process.cwd();
const NAMES_FILENAME = `人名.txt`;
const ABSOLUTE_NAMES_FILENAME = join(RUNTIME_CWD, NAMES_FILENAME);
const RENAME_NUMBER = 10;
function filterJPGFile(name) {
return /\.jpg$/.test(name);
}
(async () => {
let fsStatNames = readdirSync(RUNTIME_CWD);
if (!fsStatNames.find(str => str === NAMES_FILENAME)) {
console.log(`未找到文件[${NAMES_FILENAME}]`);
return;
}
let jpgFilenames = fsStatNames.filter(filterJPGFile),
withoutExtNames = jpgFilenames.map(str => str.replace(/\.jpg$/, '')),
namesSet = new Set(withoutExtNames),
names = readFileSync(ABSOLUTE_NAMES_FILENAME).toString().split('\n').map(str => str.trim()).filter(str => str),
files = fsStatNames.filter(str => /\.jpg$/.test(str));
if (names.length !== RENAME_NUMBER) {
console.log(`人名数量不等于${RENAME_NUMBER}个`)
return;
}
if (files.length !== RENAME_NUMBER) {
console.log(`图片数量不等于${RENAME_NUMBER}个`)
}
for (const name of names) {
if (namesSet.has(name)) {
console.log(`存在重命名情况`)
return;
}
}
for (let i = 0; i < RENAME_NUMBER; i++) {
renameSync(join(RUNTIME_CWD, files[i]), join(RUNTIME_CWD, names[i] + '.jpg'))
}
console.log(`命令执行结束`)
})();
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|