一段图片重命名的代码,运行完后如何自动关闭cmd窗口,求指教。
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), join(RUNTIME_CWD, names + '.jpg'))
}
console.log(`命令执行结束`)
})();
加一句process.exit(0); 我看不懂。我现在自己做的是在excel表格里面,写的vba,批量重命名, 尾部加
// 脚本执行完毕后退出
process.exit(0); 用python就舒服多了 Kali小弟弟 发表于 2024-5-21 13:49
用python就舒服多了
{:1_924:}用啥语言也不会默认自动关闭啊 昨天还在找来着
页:
[1]