Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created June 19, 2020 15:27
Show Gist options
  • Save senthilmpro/8d411d11a11a8d2742ed018cfc6ef3b7 to your computer and use it in GitHub Desktop.
Save senthilmpro/8d411d11a11a8d2742ed018cfc6ef3b7 to your computer and use it in GitHub Desktop.
create-folder move-files
const fs = require('fs');
const path = require('path');
const HOME_PATH = "H:\\English\\1080p-hevc";
const HOME_FOLDER = path.resolve(HOME_PATH);
const files = fs.readdirSync(HOME_FOLDER, 'utf-8');
console.log(files);
const isMatch = (str) => {
let str1 = str.replace("."," ")
let regEx1 = new RegExp(/(\w+ )+\(\d+\)/,"gi");
let matches = str.match(regEx1);
if(matches && matches.length > 0){
return matches[0];
}
return null;
}
const fileMatch = (str) => {
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);
files.forEach(fileName => {
const folderName = fileMatch(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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment