Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created January 11, 2020 00:01
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kentcdodds/59daa81d46ba51b926a6a2f044aa5ad6 to your computer and use it in GitHub Desktop.
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.
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
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