Skip to content

Instantly share code, notes, and snippets.

@jonocairns
Created January 22, 2020 06:17
Show Gist options
  • Save jonocairns/71f445d20ba03d76778963fec5b91375 to your computer and use it in GitHub Desktop.
Save jonocairns/71f445d20ba03d76778963fec5b91375 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const season = 7;
const path = `\\\\192.168.1.75\\plex\\tv\\Star Trek- Deep Space Nine\\Season ${season}`
let count = 1;
fs.readdirSync(path).forEach((file) => {
console.log(`original: ${file}`)
let episode = count > 9 ? `${count}` : `0${count}`
let reg = /e\d{1,3}?\d{1,3}-\d{1,3}/
const tryAgain = file.match(reg)
console.log(`checking if it's a double episode...`)
if (tryAgain) {
episode = count > 9 ? `${count}-${count + 1}` : `0${count}-${count + 1 > 9 ? `${count + 1}` : `0${count + 1}`}`
count++
console.log(`double episode found... setting to ${episode}`)
} else {
reg = /e\d{1,3}/
const hasMatch = file.match(reg).length > 0
if (!hasMatch) {
throw new Error('something is wrong, could not match eXXX or eXXX-XXX')
}
console.log(`normal episode found... proceeding`)
}
const after = file.replace(reg, `s0${season}e${episode}`)
fs.rename(`${path}\\${file}`, `${path}\\${after}`, (err) => {
if (err) console.log('ERROR: ' + err);
});
console.log(`converted: ${after}`)
count++;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment