Find duplicate files in a folder (first-level only) using MD5 hash:

// Name: FindDuplicate
// Supports: Mac
import "@johnlindquist/kit"
import fs from "fs"
import crypto from "crypto"
const folder = await drop();
const dir = await readdir(folder[0].path)
let content = `
| Filename | MD5 Hash |
| -------- | -------- |
`;
const hashes = {}
dir.forEach(file => {
const fullPath = `${folder[0].path}/${file}`
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) { return; }
const fileData = fs.readFileSync(fullPath)
const hash = crypto.createHash('md5').update(fileData).digest('hex')
if (hashes[hash]) {
return hashes[hash].push(file)
}
hashes[hash] = [file]
})
Object.entries(hashes).forEach(([hash, listOfFiles]) => {
if (listOfFiles.length > 1) {
listOfFiles.forEach(file => {
content += `| ${file} | ${hash.slice(0,4) + "..." + hash.slice(-4)} |\n`
})
}
})
await div(md(content))