Created
February 25, 2018 17:56
-
-
Save himanshub16/4fcd83556f0328dfdb7576781ce39571 to your computer and use it in GitHub Desktop.
Find files on your desktop and send them elsewhere (clustered by extension)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const homedir = require('os').homedir; | |
function scanDirectory(dirpath, allFiles) { | |
// nodejs runs single threaded, so is thread-safe. | |
// NO MUTEX. MISSION ACCOMPLISHED YAYY | |
if (dirpath.fullpath !== undefined) { | |
dirpath = dirpath.fullpath; | |
} | |
// had to use for...of because system calls caused exceptions as well | |
// otherwise would have used map | |
// for example, permission error for user | |
// however this should be exempted for root, which is handled well here | |
for (let eachPath of fs.readdirSync(dirpath)) { | |
try { | |
let fullpath = path.join(dirpath, eachPath); | |
let statHere = fs.statSync(fullpath); | |
let isFile = statHere.isFile() && !statHere.isSymbolicLink(); | |
let isDir = statHere.isDirectory() && !statHere.isSymbolicLink(); | |
if (isFile) { | |
allFiles.push({ | |
fullpath: fullpath, | |
filesize: sizeInMB(statHere.size) | |
}); | |
} else if (isDir) { | |
scanDirectory(fullpath, allFiles); | |
} | |
} catch (exc) { | |
console.error('error while ls for', eachPath); | |
} | |
} | |
// let everythingHere = fs.readdirSync( | |
// (dirpath.fullpath === undefined) ? dirpath : dirpath.fullpath | |
// ).map(fs.statSync); | |
// let filesHere = everythingHere.filter(each => each.isFile); | |
// let dirsHere = everythingHere.filter(each => each.isDir); | |
// console.log('files =', filesHere.length, '| dirs =', dirsHere.length); | |
// filesHere.map(allFiles.push); | |
// dirsHere.map(subdirpath => scanDirectory(subdirpath, allFiles)); | |
} | |
function sizeInMB(size) { | |
return size / (1024 * 1024); | |
} | |
function clusterFilesByExtension(allFiles) { | |
let cluster = {}; | |
for (let eachFile of allFiles) { | |
let extension = path.parse(eachFile.fullpath).ext.replace('.', ''); | |
if (cluster[extension] === undefined) { | |
cluster[extension] = []; | |
} | |
cluster[extension].push(eachFile); | |
} | |
return cluster; | |
} | |
function moveFilesTo(files, to) { | |
if (!fs.existsSync(to)) { | |
fs.mkdirSync(to); | |
} | |
files.map(each => fs.renameSync( | |
each.fullpath, | |
path.join(to, path.parse(each.fullpath).base) | |
)); | |
} | |
async function main() { | |
let rootPath = path.resolve(process.argv[2]); | |
let moveTo = path.resolve(process.argv[3] == undefined ? path.join(homedir(), 'Documents') : process.argv[3]); | |
let N = process.argv[4] === undefined ? homedir() : process.argv[4]; | |
console.log(rootPath, N, moveTo); | |
if (rootPath === undefined) { | |
console.error('Please provide a root directory to scan for'); | |
process.exit(1); | |
} | |
rootPath = path.resolve(rootPath); | |
var allFiles = []; | |
await scanDirectory(rootPath, allFiles); | |
console.log('total files', allFiles.length); | |
// cluster them by extension | |
let allFilesClustered = clusterFilesByExtension(allFiles); | |
console.log('moving files to respective directories'); | |
Object.keys(allFilesClustered).map( | |
key => moveFilesTo(allFilesClustered[key], path.join(moveTo, key)) | |
); | |
console.log('done'); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment