Skip to content

Instantly share code, notes, and snippets.

@yanneves
Last active May 15, 2021 01:03
Show Gist options
  • Save yanneves/c650680a400584b322f20435aa619076 to your computer and use it in GitHub Desktop.
Save yanneves/c650680a400584b322f20435aa619076 to your computer and use it in GitHub Desktop.
Media batch rename
{
"type": "module"
}
import fs from "fs"
const run = async () => {
const files = await fs.promises.readdir("./")
const ops = files
.filter((filename) => filename.includes(".mkv"))
.map((filename) => {
const renamed = filename
.replace(/^.+S\d+E/, "")
.replace(/\.1080p.+\.mkv/, ".mkv")
.replace(/(\d+)\./, "$1 - ")
.replace(/\./g, " ")
.replace(/\smkv$/, ".mkv")
const op = [`./${filename}`, `./${renamed}`]
return process.argv.includes("--exec")
? fs.promises.rename(...op)
: Promise.resolve(console.info(op))
})
try {
await Promise.all(ops)
console.info("All done!")
} catch (e) {
console.error(e)
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment