const path = require('path');
const os = require('os');
const fs = require('fs');
const curdir = process.argv[2];
const workingDir = path.join(__dirname, curdir);
console.log(workingDir)
if(!curdir || !fs.existsSync(workingDir)) {
console.error('해당 경로 및 폴더가 존재하지 않습니다.');
return;
}
const videoDir = path.join(workingDir, 'video');
const capturedDir = path.join(workingDir, 'captured');
const duplicatedDir = path.join(workingDir, 'duplicated');
!fs.existsSync(videoDir) && fs.mkdirSync(videoDir);
!fs.existsSync(capturedDir) && fs.mkdirSync(capturedDir);
!fs.existsSync(duplicatedDir) && fs.mkdirSync(duplicatedDir);
fs.promises
.readdir(workingDir)
.then( processFiles )
.catch(console.error);
function processFiles(files) {
files.forEach((file) => {
if ( isVideoFile(file)){
move(file, videoDir)
}else if (isCapturedFile(file)){
move(file, capturedDir)
}else if (isDuplicatedFile(file)){
move(file, duplicatedDir);
}
});
};
function isVideoFile(file) {
// g: global, 전역검색
// m: multi-line 다중, 여러줄 검색
const regExp = /(mp4|mov)$/gm;
const match = file.match(regExp);
return !!match; // 해당 값이 존재할 때 true 아니면 false;
}
function isCapturedFile(file) {
const regExp = /(png|aae)/gm;
const match = file.match(regExp);
return !!match;
}
function isDuplicatedFile(file) {
if(!file.startsWith('IMG_') || file.startsWith('IMG_E')) {
return false;
}
return true;
}
function move(file, targetDir){
console.info(`move ${file} ot ${path.basename(targetDir)}`)
const oldPath = path.join(workingDir, file);
const newPath = path.join(targetDir, file);
fs.promises
.rename(oldPath, newPath)
.catch(console.error);
}