Skip to content

Instantly share code, notes, and snippets.

@datvm
Created January 11, 2020 16:01
Show Gist options
  • Save datvm/a6603478c691b474ca0f2745d2e748ad to your computer and use it in GitHub Desktop.
Save datvm/a6603478c691b474ca0f2745d2e748ad to your computer and use it in GitHub Desktop.
A NodeJS script to rename output PNG files by PowerPoint so they can be sorted by file name
const readline = require("readline");
const fs = require("fs");
const path = require("path");
class Renamer {
input() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Enter folder: ", answer => this.renameFilesInFolder(answer));
}
renameFilesInFolder(folder) {
const files = fs.readdirSync(folder);
console.log(`${files.length} files found.`);
for (let i = 1; i <= files.length; i++) {
try {
let src = path.join(folder, `Slide${i}.PNG`);
let dest = path.join(folder, `Slide${this._appendZero(i)}.PNG`)
fs.renameSync(src, dest);
} catch (e) {
console.error(e);
}
}
process.exit(0);
}
_appendZero(i) {
let result = i.toString();
while (result.length < 5) {
result = "0" + result;
}
return result;
}
}
(function () {
new Renamer().input();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment