概述通过目录选择器与摘要计算,可以进行重复文件识别与重命名处理。遍历与重命名async function hash(file) { const buf = await crypto.subtle.digest('SHA-256', await file.arrayBuffer()); return Array.from(new Uint8Array(buf)).map(x=>x.toString(16).padStart(2,'0')).join(''); }
async function renameInDir() {
const dir = await window.showDirectoryPicker();
const map = new Map();
for await (const [name, h] of dir.entries()) {
if (h.kind !== 'file') continue;
const f = await h.getFile();
const hex = await hash(f);
if (map.has(hex)) {
const newName = `${hex}-${name}`;
const nh = await dir.getFileHandle(newName, { create: true });
const w = await nh.createWritable();
await w.write(await f.arrayBuffer());
await w.close();
await dir.removeEntry(name);
} else { map.set(hex, name); }
}
}

发表评论 取消回复