Created
April 13, 2017 09:27
-
-
Save PgBiel/336af9012247c81d5e6eb03ed6c822e6 to your computer and use it in GitHub Desktop.
Quick Renaming of files of a directory (Node 7+)
This file contains hidden or 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
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