Skip to content

Instantly share code, notes, and snippets.

@gauravchl
Created August 20, 2016 10:16
Show Gist options
  • Save gauravchl/93a5ecf8023bbdd09e09448da75aa40f to your computer and use it in GitHub Desktop.
Save gauravchl/93a5ecf8023bbdd09e09448da75aa40f to your computer and use it in GitHub Desktop.
A simple javascript to rename the .srt filename to it's movie file. Note: currently support .mkv file only & both mkv and srt should be in same directory.
'use strict';
const fs = require('fs');
const path = process.argv[2];
console.log('\n\nRunning script...');
if (!path) {
console.log('Please provide a path');
process.exit();
}
function startProcessing() {
console.log(`Finding directories inside ${path}`);
var AllFilesAndDir = fs.readdirSync(path);
var directories = AllFilesAndDir.filter((name) => name.indexOf('.') < 0)
if (!directories || !directories.length) return console.log('No directories found inside given path');
console.log(`${directories.length} directories found -> `, directories.join(', '), '\n\n');
directories.forEach((dir) => {
let files = fs.readdirSync(path + dir);
let movieFile = files.find((item) => item.match(/.mkv$/));
let srtFile = files.find((item) => item.match(/.srt$/));
if (movieFile && srtFile) renameFile(path, dir, srtFile, movieFile);
else console.log(`No srt or .mkv file found inside ${dir}`);
})
}
function renameFile(path, dir, srtFileName, movieFileName) {
let oldFileName = srtFileName;
let newFileName = movieFileName.slice(0, movieFileName.indexOf('.mkv')) + '.srt';
console.log(`srt and mkv files found inside ${dir}`);
console.log(` Renaming '${oldFileName}' to '${newFileName}'`);
fs.renameSync(path + dir + '/' + oldFileName, path + dir + '/' + newFileName);
};
startProcessing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment