Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created December 7, 2020 00:57
Show Gist options
  • Save senthilmpro/8dbe0998aebe5bd3e8dd10df7b167489 to your computer and use it in GitHub Desktop.
Save senthilmpro/8dbe0998aebe5bd3e8dd10df7b167489 to your computer and use it in GitHub Desktop.
create-movie-folder.js - Create Movie folder to support Plex/Emby
const fs = require('fs');
const path = require('path');
const HOME_PATH = "I:\\dl";
const HOME_FOLDER = path.resolve(HOME_PATH);
const files = fs.readdirSync(HOME_FOLDER, 'utf-8');
console.log(files);
const fileMatchDots = (str) => {
let regExp = /^(.+?)[.( \t]*(?:(19\d{2}|20(?:0\d|1[0-9])).*|(?:(?=bluray|\d+p|brrip|webrip)..*)?[.](mkv|avi|mpe?g|mp4)$)/gi;
let matches = regExp.exec(str);
if(matches && matches.length > 0){
return {
name : matches[1],
year : matches[2],
file : matches[0]
}
}
return null;
}
const fileMatchDotsNew = (str) => {
let regExp = /(\S*.+)\.(19\d{2}|20\d{2})/gi;
let matches = regExp.exec(str);
if(matches && matches.length > 0){
let year = matches[2];
let name = matches[1].replace(".", " ");
let folderName = `${name} (${year})`
return {
name : name,
year : year,
folderName : folderName
}
}
return null;
}
const fileMatchOriginal = (str) => {
let regExp = /(.+)\W(\d{4})\)/gi;
//let regExp = /^(.+?)[.( \t]*(?:(19\d{2}|20(?:0\d|1[0-9])))\)/gi;
let matches = regExp.exec(str);
if(matches && matches.length > 0){
console.log(matches[0]);
return matches[0];
}
return null;
}
const fileMatchBrackets = (str) => {
let regExp = /(.+)(\W*)\[\d{4}\]/gi;
//let regExp = /^(.+?)[.( \t]*(?:(19\d{2}|20(?:0\d|1[0-9])))\)/gi;
let matches = regExp.exec(str);
if(matches && matches.length > 0){
console.log(matches[0]);
return matches[0];
}
return null;
}
const createFolder = (str) => {
if(!fs.existsSync(str)){
fs.mkdirSync(str);
}
}
const moveFile = (originalPath, newPath) => {
try {
fs.rename(originalPath, newPath, function (err) {
if (err) throw err
console.log('Successfully renamed - AKA moved! ')
})
} catch (err) {
console.log("ERROR : ", err);
}
}
console.log("HOME_FOLDER ", HOME_FOLDER);
const moveFileDotsNew = () => {
files.forEach(fileName => {
const matchObject = fileMatchDotsNew(fileName);
let folderName = null;
if(matchObject && matchObject["name"] && matchObject["year"]){
let name = matchObject["name"].replace(/[,.]/g, " ").replace("[","");
folderName = `${name} (${matchObject["year"]})`
}
if(folderName){
const fullPath = path.resolve(HOME_FOLDER, folderName);
console.log(fullPath);
createFolder(fullPath);
let originalPath = path.resolve(HOME_FOLDER, fileName);
let newPath = path.resolve(HOME_FOLDER, folderName, fileName);
moveFile(originalPath, newPath);
}
});
}
const moveFileDots = () => {
files.forEach(fileName => {
const matchObject = fileMatchDots(fileName);
let folderName = null;
if(matchObject && matchObject["name"] && matchObject["year"]){
let name = matchObject["name"].replace(/[,.]/g, " ").replace("[","");
folderName = `${name} (${matchObject["year"]})`
}
if(folderName){
const fullPath = path.resolve(HOME_FOLDER, folderName);
console.log(fullPath);
createFolder(fullPath);
let originalPath = path.resolve(HOME_FOLDER, fileName);
let newPath = path.resolve(HOME_FOLDER, folderName, fileName);
moveFile(originalPath, newPath);
}
});
}
const moveFileOriginal = () => {
files.forEach(fileName => {
const folderName = fileMatchOriginal(fileName);
if(folderName){
const fullPath = path.resolve(HOME_FOLDER, folderName);
console.log(fullPath);
createFolder(fullPath);
let originalPath = path.resolve(HOME_FOLDER, fileName);
let newPath = path.resolve(HOME_FOLDER, folderName, fileName);
moveFile(originalPath, newPath);
}
});
}
const moveFileBrackets = () => {
files.forEach(fileName => {
const folderName = fileMatchBrackets(fileName);
if(folderName){
const fullPath = path.resolve(HOME_FOLDER, folderName);
console.log(fullPath);
createFolder(fullPath);
let originalPath = path.resolve(HOME_FOLDER, fileName);
let newPath = path.resolve(HOME_FOLDER, folderName, fileName);
moveFile(originalPath, newPath);
}
});
}
const TYPE = {
ORIGINAL : "Original",
DOTS : "Dots",
BRACKETS : "Brackets",
DOTS_NEW : "Dots New"
};
function main(type) {
if(type === TYPE.ORIGINAL){
moveFileOriginal();
} else if(type === TYPE.DOTS){
moveFileDots();
} else if(type === TYPE.DOTS_NEW) {
moveFileDotsNew();
} else if(type === TYPE.BRACKETS) {
moveFileBrackets();
}
}
main(TYPE.DOTS_NEW);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment