Created
December 2, 2020 04:30
-
-
Save Gunslap/b3cceb350229cd0065142e47433ab745 to your computer and use it in GitHub Desktop.
Rename Photos Downloaded from Google Photos Using Takeout
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
#Google Photos File Renamer: | |
#just a quick script to rename your jpg's downloaded from google photos after their subfolder's name | |
#(the date they were uploaded if you used Google Takeout) while adding an incrementing number | |
#ie: "(1)" like explorer does if the filename already exists | |
#Path to Google Photos directory (should have subdirectories named as dates) | |
$path = Get-ChildItem -Path "D:\Downloads\Takeout\Google Photos" | |
#Loop through the subfolders | |
foreach ($folder in $path) { | |
#get the name of the current subfolder which we will use to rename the files inside | |
$name = $folder.PSChildName | |
#reset the counter to null | |
$i = $null | |
#spit out the current folder name so we know where we are | |
$name | |
#Get the jpeg's in the current subfolder | |
$jpg = Get-ChildItem -Path $folder | where { $_.Extension -eq ".jpg" -or $_.Extension -eq ".jpeg" } | |
#Loop through the Jpg's | |
foreach ($item in $jpg) { | |
$oldName = $item.PSChildName | |
#If the current jpg is already named correctly, move on | |
if ($oldName -notmatch $name) { | |
$oldFullName = $item.FullName | |
$renamed = $false | |
#make a version of the current filename with escaped brackets for regex: | |
$testName = ($oldName -replace "\(","\(") -replace "\)","\)" | |
#check if there is already a jpg named after the directory or not | |
if (Test-Path ($oldFullName -replace $testName, "$name.jpg")) { | |
#if this is the first number photo in the folder, set the counter to 1 | |
if($i -eq $null){$i = 1} | |
#try adding a number at the end and keep incrementing it until you find a file name | |
#that doesn't already exist | |
while ($renamed -eq $false) { | |
if ((Test-Path ($oldFullName -replace $testName, "$name ($i).jpg"))) { | |
$i++ | |
} | |
else { | |
#we found a number that doesn't already exist, rename it! | |
$item | rename-item -NewName "$name ($i).jpg" | |
$renamed = $true | |
} | |
} | |
} | |
else { | |
#There isn't already a jpg named after the directory, so rename it! | |
$item | rename-item -NewName "$name.jpg" | |
} | |
#If we didn't need to add a number the new filename... | |
if ($i -eq $null) { | |
#Check if a related .json file exists, and if so rename it as well | |
if (Test-Path "$oldFullName.json") { | |
Rename-Item -Path "$oldFullName.json" -NewName "$name.jpg.json" | |
} | |
} | |
#if we did add a number to the new file name... | |
else { | |
#Check if a related .json file exists, and if so rename it as well | |
if (Test-Path ("$oldFullName.json")) { | |
Rename-Item -Path "$oldFullName.json" -NewName "$name ($i).jpg.json" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently I have it scoped down to just JPG's, but there's no reason it couldn't also handle the other possible file types.