Created
January 16, 2023 17:19
-
-
Save Hashbrown777/a504cc2ea59aadf54ef77b6e664f00c9 to your computer and use it in GitHub Desktop.
Sorting captures from holidays into folders named by date
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
$ErrorActionPreference = 'Stop' | |
$shell = New-Object -ComObject 'Shell.Application' | |
&{ | |
gci -File ` | |
| ?{ | |
#leave these in the root directory | |
$_.Name -notmatch '^(IMG_\d{4}.JPG|sort.ps1|Untitled.png)$' | |
} | |
gci -Directory ` | |
| ?{ | |
#pickup items I did sort of organise | |
$_.Name -match '^(gopro\d|Screenshots)$' | |
} ` | |
| gci -File | |
} ` | |
| %{ | |
$file = $_ | |
$dir = $shell.NameSpace($file.Directory.FullName) | |
if ($dir.GetDetailsOf($NULL, 208) -ne 'Media created') { | |
throw $dir.GetDetailsOf($NULL, 208) | |
} | |
$date = switch -regex ($file.Name) { | |
#phones did a good job of naming their files (2022 Dec, to 2023 Jan) | |
'(?<=_)202(212|301)\d\d(?=[-_])' { | |
$Matches[0] | |
} | |
#items saved off facebook we have to rely on datetimes | |
#which, at the home pc, appear to be off by the time difference from where the device was when it was saved | |
'^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}.jpg$' { | |
$file.LastWriteTime.AddHours(-3).ToString('yyyyMMdd') | |
} | |
#this digital camera appears to have the opposite issue..somehow | |
'^DSCN\d{4}.JPG$' { | |
$file.LastWriteTime.AddHours(3).ToString('yyyyMMdd') | |
} | |
#the gopro we needed to rely on media metadata, and it must just its clock set wrong | |
'^G(OPR|P\d{2})\d{4}.MP4$' { | |
[datetime]::ParseExact( | |
( | |
$dir.GetDetailsOf($dir.ParseName($_), 208) -replace "$([char]8206)|$([char]8207)",'' | |
), | |
'd/MM/yyyy h:mm tt', | |
$NULL | |
).AddHours(-14).ToString('yyyyMMdd') | |
} | |
} | |
if (!$date) { | |
throw $_.FullName | |
} | |
if (!(Test-Path $date)) { | |
New-Item -Type Directory -Name $date | Out-Null | |
} | |
elseif (Test-Path ($date + '/' + $_.Name)) { | |
throw $_.FullName | |
} | |
$_ | Move-Item -Destination $date | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment