Created
January 11, 2020 00:01
-
-
Save kentcdodds/59daa81d46ba51b926a6a2f044aa5ad6 to your computer and use it in GitHub Desktop.
Just a script I wrote to get my downloaded Google Photos into the right place.
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 path = require('path') | |
const fs = require('fs') | |
const execSync = require('child_process').execSync | |
if (!process.argv[2] || !process.argv[3]) { | |
throw new Error('you did not pass the source and destination paths') | |
} | |
const searchPath = path.join(process.cwd(), process.argv[2]) | |
const destination = path.join(process.cwd(), process.argv[3]) | |
console.log(`moving photos from "${searchPath}" to "${destination}"`) | |
console.log(`find "${searchPath}" -type f`) | |
const files = execSync(`find "${searchPath}" -type f`) | |
.toString() | |
.split('\n') | |
.map(f => f.trim()) | |
.filter(Boolean) | |
.forEach(file => { | |
const destFile = getDestFile(file) | |
execSync(`mkdir -p "${path.dirname(destFile)}"`) | |
console.log(`mv "${file}" "${destFile}"`) | |
execSync(`mv "${file}" "${destFile}"`) | |
}) | |
console.log('done') | |
function getDestFile(file, count = 0) { | |
const relativeFilepath = file.replace(searchPath, '') | |
let destFile = path.join(destination, relativeFilepath) | |
let {dir, name, ext} = path.parse(destFile) | |
if (count > 0) { | |
name = `${name}-${count}` | |
} | |
const parentDir = dir.split('/').slice(-1) | |
// some of the folders are actual albums and others are just google-created albums | |
if (/^\d{4}/.test(parentDir)) { | |
dir = destination | |
} | |
destFile = path.format({dir, name, ext}) | |
if (fs.existsSync(destFile)) { | |
destFile = getDestFile(file, count + 1) | |
} | |
return destFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
💯 Thank you for sharing @kentcdodds!
(Reading
.filter(Boolean)
feels really good)