Skip to content

Instantly share code, notes, and snippets.

@PgBiel
Created April 13, 2017 09:27
Show Gist options
  • Save PgBiel/336af9012247c81d5e6eb03ed6c822e6 to your computer and use it in GitHub Desktop.
Save PgBiel/336af9012247c81d5e6eb03ed6c822e6 to your computer and use it in GitHub Desktop.
Quick Renaming of files of a directory (Node 7+)
if (require.main !== module) throw new Error("Must not be require()d");
const fs = require("fs");
process.stdin.setEncoding("utf8");
process.openStdin();
const promisifyEvent = (emitter, event, handler, onOnce) => {
return new Promise((res, rej) => {
const newFunc = function() {
const args = Array.from(arguments);
res(handler.apply(handler, args));
};
emitter[onOnce](event, newFunc);
});
};
(async function(){
console.log("Path to use?");
// Here you can specify a path to use instead of the actual directory
// To stay in the current directory just press enter without anything else
process.chdir(await promisifyEvent(process.stdin, "data", data=>{
if (data.replace(/\s/g, "")) return data.replace(/\n/g, "");
else return "./";
}, "once"));
for (const file of fs.readdirSync("./")) {
// Log file name and await new name
console.log(file);
await promisifyEvent(process.stdin, "data", data=>{
if (data.replace(/\s/g, "")) fs.renameSync(`./${file}`, `./${data.replace(/\n/g, "")}`);
}, "once");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment