Skip to content

Instantly share code, notes, and snippets.

@rpc180
Last active December 3, 2023 16:56
Show Gist options
  • Save rpc180/24e560c7eece19623f87c21bdcd51566 to your computer and use it in GitHub Desktop.
Save rpc180/24e560c7eece19623f87c21bdcd51566 to your computer and use it in GitHub Desktop.
Google Photos JSON Date Sorter
# Can run from anywhere
# Photos coded to be present in D:\photos subfolders
# Will also work on mp4 video files if present
# Folders labeled with dates will be created at script execution based on first photo with new date field
# Photos will be copied to D:\photos\date-taken
$startscript = get-date
$shortdate = get-date -format MM-dd-yy
$systemrunning = hostname
$runpath = get-location
$logfile = $runpath.path+"\runlog_"+$shortdate+".txt"
Write-output "Starting script at $startscript" | Tee-object -filepath $logfile
Write-output "log file location: $logfile" | Tee-Object -filepath $logfile -append
Write-output "This script is executing on $systemrunning" | Tee-Object -filepath $logfile -append
Write-output "in directory $runpath"
add-content $logfile "`r"
$alljson = gci d:\photos -recurse -filter *.json
Function Extract-ImageDate {
$filepath = $json.DirectoryName
$attributes = gc $json.FullName | ConvertFrom-Json
$filename = $attributes.title
$UNIXDate=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($($attributes.photoTakenTime.timestamp)))
$PhotoTakenDate = $UNIXDate.Year.tostring()+"-"+$UnixDate.Month.tostring("00")+"-"+$unixDate.Day.tostring("00")
write-output "JSON Taken Date: $PhotoTakenDate" | tee-object $logfile -append
write-output "Image file: $filename" | Tee-object $logfile -append
If (-not (Test-Path "D:\photos\$phototakendate")) {
# Folder doesn't exist yet, create
write-output "Creating folder D:\photos\$phototakendate" | tee-object $logfile -append
New-Item -path "D:\photos\$phototakendate" -ItemType Directory
# Copy current file to newly created folder
write-output "Copying $filename to $phototakendate" `r`n | Tee-object $logfile -append
$originalimagefile = $filepath+"\"+$filename
copy-item $originalimagefile "D:\photos\$phototakendate"
}
Else {
write-output "Copying $filename to existing $phototakendate" `r`n | tee-object $logfile -append
$originalimagefile = $filepath+"\"+$filename
copy-item $originalimagefile "D:\photos\$phototakendate"
}
}
ForEach ( $json in $alljson ) {
write-output "Processing $json" | tee-object $logfile -Append
Extract-ImageDate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment