Skip to content

Instantly share code, notes, and snippets.

@AL1L
Created March 8, 2019 17:15
Show Gist options
  • Save AL1L/77ae11c05831fb96734ef22e997901ea to your computer and use it in GitHub Desktop.
Save AL1L/77ae11c05831fb96734ef22e997901ea to your computer and use it in GitHub Desktop.
Sorts files based on type (which is based on the file extension). Edit the `toSort`, `sort`, `types` and `ext` vars to change how you want it to be sorted. As I find more file extensions to add ill add them, also comment what you think should be added
const fs = require('fs');
const path = require('path');
function mkDirByPathSync(targetDir, { isRelativeToScript = false } = {}) {
const sep = path.sep;
const initDir = path.isAbsolute(targetDir) ? sep : '';
const baseDir = isRelativeToScript ? __dirname : '.';
return targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir);
try {
fs.mkdirSync(curDir);
} catch (err) {
if (err.code === 'EEXIST') { // curDir already exists!
return curDir;
}
// To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows.
if (err.code === 'ENOENT') { // Throw the original parentDir error on curDir `ENOENT` failure.
throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`);
}
const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1;
if (!caughtErr || caughtErr && curDir === path.resolve(targetDir)) {
throw err; // Throw if it's just the last created dir.
}
}
return curDir;
}, initDir);
}
const toSort = "C:\\Users\\AL_1\\Documents\\Sortedn't";
const sorted = "C:\\Users\\AL_1\\Documents\\Sorted";
const types = {
"image": "Media\\Images",
"video": "Media\\Videos",
"audio": "Media\\Audio",
"bin": "Binarys",
"zip": "Archives",
"folder": "Folders",
"link": "Links",
"docs": "Documents",
"keys": "Keys",
"fonts": "Fonts",
"cur": "Cursors",
"scripts": "Scripts",
"log": "Logs",
"flash": "Flash",
"speech": "Speech",
// Langs
"py": "Langs\\Python",
"js": "Langs\\JavaScript",
"sql": "Langs\\SQL",
"json": "Langs\\JSON",
"html": "Langs\\HTML",
"md": "Langs\\Markdown",
"yaml": "Langs\\YAML",
}
const ext = {
// images
".png": "image",
".jpg": "image",
".jpeg": "image",
".gif": "image",
".webm": "image",
".svg": "image",
// Videos
".mp4": "video",
".mov": "video",
".avi": "video",
// Audio
".mp3": "audio",
".wav": "audio",
// Binarys
".exe": "bin",
".dll": "bin",
".bin": "bin",
".jar": "bin",
".msi": "bin",
// Archives
".zip": "zip",
".rar": "zip",
".7z": "zip",
".tar": "zip",
".gz": "zip",
".bz": "zip",
".bz2": "zip",
// Documents
".pdf": "docs",
".doc": "docs",
".docx": "docs",
".txt": "docs",
// Keys
".pub": "keys",
".key": "keys",
".ppk": "keys",
// Fonts
".ttf": "fonts",
".wolf": "fonts",
// Other
".cur": "cur",
".log": "log",
".lnk": "link",
".url": "link",
".swf": "flash",
".ssml": "speech",
// Scripts
".bat": "scripts",
".cmd": "scripts",
".sh": "scripts",
// Langs
".py": "py",
".pyc": "py",
".py3": "py",
".js": "js",
".map": "js",
".sql": "sql",
".json": "json",
".json5": "json",
".html": "html",
".yml": "yaml",
".yaml": "yaml",
".md": "md",
}
fs.readdir(toSort, (err, files) => {
if(err) return;
files.forEach((file) => {
try {
const absPath = path.join(toSort, file);
const stat = fs.statSync(absPath);
let folder;
if(stat.isSymbolicLink()) {
folder = path.join(sorted, types.link);
} else if (stat.isDirectory()) {
folder = path.join(sorted, types.folder);
} else {
const fileExt = path.extname(absPath);
if(!(fileExt in ext)) return;
folder = path.join(sorted, types[ext[fileExt]])
}
if(!folder) return;
if(!fs.existsSync(folder))
mkDirByPathSync(folder);
fs.renameSync(absPath, path.join(folder, file));
console.log("Moved " + file)
} catch(e) {
console.error(e.message, e.stack)
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment