Last active
April 23, 2018 12:31
-
-
Save uqmessias/8923627bc1e9846250baf2825ff9ed8c to your computer and use it in GitHub Desktop.
Script to rename and organize all files from my Onedrive photo folder
This file contains hidden or 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 albumPath = '/home/uqmessias/OneDrive/Imagens/Álbum\ da\ Câmera'; | |
const albumNewPath = '/home/uqmessias/development/rename-files/files'; | |
const handleFile = (dirPath, fileName) => { | |
const fileOldPath = `${dirPath}/${fileName}`; | |
const stats = fs.statSync(fileOldPath); | |
const fileNewPath = `${albumNewPath}/${fileName}`; | |
const atime = new Date(stats.atimeMs); | |
const mtime = new Date(stats.mtimeMs); | |
if (stats.isDirectory()) { | |
organizeFiles(fileOldPath); | |
} else if (!fs.existsSync(fileNewPath)) { | |
console.log(`Copying file from "${fileOldPath}" to "${fileNewPath}"...`); | |
fs.copyFileSync(fileOldPath, fileNewPath); | |
fs.utimesSync(fileNewPath, atime, mtime); | |
} | |
}; | |
const organizeFiles = (dirPath) => { | |
console.log(`Reading dir "${dirPath}" content ...`); | |
fs.readdir(dirPath, (err, fileNames) => { | |
let counter = 0; | |
fileNames.forEach(fileName => handleFile(dirPath, fileName)); | |
}); | |
}; | |
organizeFiles(albumPath); | |
console.log('Done!'); |
This file contains hidden or 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 albumPath = '/home/uqmessias/OneDrive/Imagens/Álbum\ da\ Câmera'; | |
const getTwoDigits = numeric => { | |
const digits = `0${numeric}`; | |
return digits.substr(digits.length - 2, 2); | |
}; | |
const monthNamesPt = [ | |
'Janeiro', 'Fevereiro', 'Março', | |
'Abril', 'Maio', 'Junho', | |
'Julho', 'Agosto', 'Setembro', | |
'Outubro', 'Novembro', 'Dezembro', | |
]; | |
const handleFile = fileName => { | |
const stats = fs.statSync(`${albumPath}/${fileName}`); | |
let mtime = new Date(stats.mtimeMs); | |
const atime = new Date(stats.atimeMs); | |
const monthWithTwoDigits = getTwoDigits(mtime.getMonth() + 1); | |
const dayWithTwoDigits = getTwoDigits(mtime.getDate()); | |
const hourWithTwoDigits = getTwoDigits(mtime.getHours()); | |
const minuteWithTwoDigits = getTwoDigits(mtime.getMinutes()); | |
const i = n => parseInt(n, 10); | |
const arrayExec = /_([0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2})_?([0-9]{2})/gi.exec(fileName); | |
const [_, fullYear, month, day, hour, minute] = arrayExec || | |
[undefined, mtime.getFullYear(), monthWithTwoDigits, dayWithTwoDigits, hourWithTwoDigits, minuteWithTwoDigits]; | |
if (fullYear != mtime.getFullYear() || month !== monthWithTwoDigits) { | |
mtime = new Date(i(fullYear), i(month) - 1, i(day), i(hour), i(minute)); | |
console.log(`from ${stats.mtime.toISOString()} to ${mtime.toISOString()}`); | |
} | |
const yearPath = `${albumPath}/${mtime.getFullYear()}`; | |
const monthPath = `${yearPath}/${getTwoDigits(month)} - ${monthNamesPt[i(month) - 1]}`; | |
const fileOldPath = `${albumPath}/${fileName}`; | |
const filePath = `${monthPath}/${fileName}`; | |
if (!fs.existsSync(yearPath)) { | |
console.log(`Creating a new directory ${yearPath}...`); | |
fs.mkdirSync(yearPath); | |
} | |
if (!fs.existsSync(monthPath)) { | |
console.log(`Creating a new directory ${monthPath}...`); | |
fs.mkdirSync(monthPath); | |
} | |
if (!fs.existsSync(filePath) && stats.isFile()) { | |
console.log(`Moving file from "${fileOldPath}" to "${filePath}"...`); | |
fs.renameSync(fileOldPath, filePath); | |
fs.utimesSync(filePath, atime, mtime); | |
} | |
}; | |
const organizeFiles = () => { | |
fs.readdir(albumPath, (err, fileNames) => { | |
console.log(`${fileNames.length} files found`); | |
console.log('Creating directories based on files information...'); | |
fileNames.forEach(handleFile); | |
console.log('Done!'); | |
}); | |
}; | |
organizeFiles(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment