Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Just a script I wrote to get my downloaded Google Photos into the right place.
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
}
@lautapercuspain

This comment has been minimized.

Copy link

lautapercuspain commented Feb 18, 2020

💯 Thank you for sharing @kentcdodds!
(Reading .filter(Boolean) feels really good)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.