Created
June 19, 2020 15:27
-
-
Save senthilmpro/8d411d11a11a8d2742ed018cfc6ef3b7 to your computer and use it in GitHub Desktop.
create-folder move-files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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