2021-07-15 20:13:25 +02:00
|
|
|
|
<#
|
2021-07-15 17:45:59 +02:00
|
|
|
|
.SYNOPSIS
|
|
|
|
|
copy-photos-sorted.ps1 [<source-dir>] [<target-dir-tree>]
|
|
|
|
|
.DESCRIPTION
|
2021-08-29 17:50:03 +02:00
|
|
|
|
Copies all photos in source-dir sorted by year and month into the target-dir-tree.
|
2021-07-15 17:45:59 +02:00
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> .\copy-photos-sorted.ps1 C:\MyPhotos C:\MyPhotoAlbum
|
2021-08-29 17:50:03 +02:00
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz · License: CC0
|
2021-07-15 17:45:59 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
#>
|
|
|
|
|
|
|
|
|
|
param([string]$SourceDir = "", [string]$TargetDirTree = "")
|
|
|
|
|
|
2021-07-15 20:13:25 +02:00
|
|
|
|
function CopyFile { param([int]$Num, [string]$SrcPath, [string]$Filename, [int]$Date, [string]$DstDir)
|
2021-07-15 17:45:59 +02:00
|
|
|
|
[int]$Year = $Date / 10000
|
|
|
|
|
[int]$Month = ($Date / 100) % 100
|
|
|
|
|
$MonthDir = switch($Month) {
|
|
|
|
|
1 {"01 JAN"}
|
|
|
|
|
2 {"02 FEB"}
|
|
|
|
|
3 {"03 MAR"}
|
|
|
|
|
4 {"04 APR"}
|
|
|
|
|
5 {"05 MAY"}
|
|
|
|
|
6 {"06 JUN"}
|
|
|
|
|
7 {"07 JUL"}
|
|
|
|
|
8 {"08 AUG"}
|
|
|
|
|
9 {"09 SEP"}
|
|
|
|
|
10 {"10 OCT"}
|
|
|
|
|
11 {"11 NOV"}
|
|
|
|
|
12 {"12 DEC"}
|
|
|
|
|
}
|
2021-07-15 19:54:00 +02:00
|
|
|
|
$DstPath = "$DstDir/$Year/$MonthDir/$Filename"
|
|
|
|
|
if (test-path "$DstPath" -pathType leaf) {
|
2021-07-15 20:13:25 +02:00
|
|
|
|
"#$($Num): $Filename exists already, skipping..."
|
2021-07-15 19:54:00 +02:00
|
|
|
|
} else {
|
2021-07-15 20:13:25 +02:00
|
|
|
|
"#$($Num): $Filename is copied..."
|
2021-07-15 19:54:00 +02:00
|
|
|
|
new-item -path "$DstDir" -name "$Year" -itemType "directory" -force | out-null
|
|
|
|
|
new-item -path "$DstDir/$Year" -name "$MonthDir" -itemType "directory" -force | out-null
|
|
|
|
|
copy-item "$SrcPath" "$DstPath" -force
|
|
|
|
|
}
|
2021-07-15 17:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if ($SourceDir -eq "") { $SourceDir = read-host "Enter source directory" }
|
|
|
|
|
if ($TargetDirTree -eq "") { $TargetDirTree = read-host "Enter target directory tree" }
|
|
|
|
|
|
|
|
|
|
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
|
|
|
|
$Files = (get-childItem "$SourceDir\*.jpg" -attributes !Directory)
|
2021-07-15 20:13:25 +02:00
|
|
|
|
"Found $($Files.Count) photos in 📂$SourceDir..."
|
|
|
|
|
[int]$Num = 0
|
2021-07-15 17:45:59 +02:00
|
|
|
|
foreach ($File in $Files) {
|
2021-07-15 20:13:25 +02:00
|
|
|
|
$Num++
|
2021-07-15 17:45:59 +02:00
|
|
|
|
$Filename = (get-item "$File").Name
|
|
|
|
|
if ("$Filename" -like "IMG_*_*.jpg") {
|
|
|
|
|
$Array = $Filename.split("_")
|
2021-07-15 20:13:25 +02:00
|
|
|
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
2021-07-15 17:58:15 +02:00
|
|
|
|
} elseif ("$Filename" -like "IMG-*-*.jpg") {
|
|
|
|
|
$Array = $Filename.split("-")
|
2021-07-15 20:13:25 +02:00
|
|
|
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
2021-07-15 17:45:59 +02:00
|
|
|
|
} elseif ("$Filename" -like "PANO_*_*.jpg") {
|
|
|
|
|
$Array = $Filename.split("_")
|
2021-07-15 20:13:25 +02:00
|
|
|
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
2021-07-15 17:58:15 +02:00
|
|
|
|
} elseif ("$Filename" -like "PANO-*-*.jpg") {
|
|
|
|
|
$Array = $Filename.split("-")
|
2021-07-15 20:13:25 +02:00
|
|
|
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
2021-07-15 17:45:59 +02:00
|
|
|
|
} else {
|
2021-07-15 20:13:25 +02:00
|
|
|
|
"#$($Num): $Filename with unknown filename format"
|
2021-07-15 17:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
2021-07-15 20:13:25 +02:00
|
|
|
|
"✔️ copied $Num photos sorted by year and month to 📂$TargetDirTree in $Elapsed sec"
|
2021-07-15 17:45:59 +02:00
|
|
|
|
exit 0
|
|
|
|
|
} catch {
|
|
|
|
|
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|