Skip to content

Instantly share code, notes, and snippets.

@kirbee
Created November 18, 2020 20:21
Show Gist options
  • Save kirbee/a6673cf10f132683152a6a02dda21263 to your computer and use it in GitHub Desktop.
Save kirbee/a6673cf10f132683152a6a02dda21263 to your computer and use it in GitHub Desktop.
A quick node script I used to re-organize some documents in the way I wanted them before uploading them to Google Drive
const testFolder = './'; // to be run in the directory with all files we are looking to move
const fs = require('fs');
fs.readdirSync(testFolder).forEach((file) => {
const regex = /SL([0-9]*) Unit([0-9]*) ([0-9\.]*)/; // eg "SL001 Unit403 2005.11.30.pdf"
const test = regex.exec(file);
if (test) {
const sl = test[1]; // First matching group
const unit = test[2]; // Second matching group
const date = test[3];
const dateHyphen = date.split('.').join('-').slice(0, -1);
// Dirname "403 - SL001/Assignment Forms"
const foldername = `${unit} - SL${sl}/Assignment Forms`;
// Filename "403 - SL001/Assignment Forms/Assignment Form 2005-11-30"
const filename = `${foldername}/Assignment Form ${dateHyphen}`;
// Uncomment to run, make sure to test first!
// fs.mkdirSync(foldername, { recursive: true });
// fs.renameSync(file, filename);
console.log(filename);
}
});
@kirbee
Copy link
Author

kirbee commented Nov 18, 2020

Yeah, it's ugly ... but it works (and it's better than manually creating every folder and drag/dropping :puke:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment