Skip to content

Instantly share code, notes, and snippets.

@sadortun
Forked from bushev/script.js
Created December 30, 2020 09:26
Show Gist options
  • Save sadortun/487af1ded1ebee2498baaf3c6baabe57 to your computer and use it in GitHub Desktop.
Save sadortun/487af1ded1ebee2498baaf3c6baabe57 to your computer and use it in GitHub Desktop.
Remove duplicated files from Moments (Synology NAS)
// Very fast-coded deduplication utility. It parses standard "Storage Analyzer" report and moves duplicated files to a separate folder.
// Requires "npm i csv-parse 'move-file"
'use strict';
const path = require('path')
const fs = require('fs')
const parse = require('csv-parse/lib/sync')
const moveFile = require('move-file');
const records = parse(fs.readFileSync('/<PATH_TO_REPORT>/duplicate_file.csv'), {
columns: true,
skip_empty_lines: true,
delimiter: '\t'
})
const MOVE_TO = '/Volumes/homes/<USER_NAME>/Drive/Moments_Duplicates'
const groups = {};
(async () => {
for (const record of records) {
if (!record.File.startsWith('/volume1/homes/<USER_NAME>/Drive/Moments')) {
console.log(`Skip file: ${record.File}`)
}
const realFilePath = record.File.replace('volume1', 'Volumes')
if (groups[record.Group]) {
const destination = path.join(MOVE_TO, path.basename(realFilePath));
// Move file
console.log(`Move: "${realFilePath}" -> "${destination}"`)
try {
await moveFile(realFilePath, destination);
// break // <--- Uncomment to move 1 file
} catch (err) {
if (err.code !== 'ENOENT') { // already moved?
throw err
}
}
} else {
groups[record.Group] = true
console.log(`Ignore file: ${record.File}`)
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment