PowerShell/scripts/copy-photos-sorted.ps1

97 lines
3.5 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2021-07-15 17:45:59 +02:00
.SYNOPSIS
2022-01-08 15:01:27 +01:00
Copy photos sorted by year and month
2021-07-15 17:45:59 +02:00
.DESCRIPTION
2024-08-13 14:04:34 +02:00
This PowerShell script copies image files from <sourceDir> to <targetDir> sorted by year and month.
2023-09-16 12:22:47 +02:00
.PARAMETER sourceDir
2022-03-28 13:06:32 +02:00
Specifies the path to the source folder
2023-09-16 12:22:47 +02:00
.PARAMTER targetDir
2021-10-16 16:50:10 +02:00
Specifies the path to the target folder
2021-07-15 17:45:59 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./copy-photos-sorted.ps1 D:\iPhone\DCIM C:\MyPhotos
2024-08-13 14:04:34 +02:00
Copying IMG_20240903_134445.jpg to C:\MyPhotos\2024\09 SEP\...
2024-10-01 13:37:53 +02:00
Copied 1 photo (0 skipped) to 📂C:\MyPhotos in 41s.
2021-07-15 17:45:59 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-03-28 13:06:32 +02:00
Author: Markus Fleschutz | License: CC0
2021-07-15 17:45:59 +02:00
#>
2023-09-16 12:22:47 +02:00
param([string]$sourceDir = "", [string]$targetDir = "")
2021-07-15 17:45:59 +02:00
2023-09-16 12:22:47 +02:00
function CopyFile { param([string]$sourcePath, [string]$targetDir, [int]$date, [string]$filename)
[int]$year = $date / 10000
[int]$month = ($date / 100) % 100
$monthDir = switch($month) {
2021-07-15 17:45:59 +02:00
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"}
}
2023-09-16 12:22:47 +02:00
$TargetPath = "$targetDir/$year/$monthDir/$filename"
2023-01-07 17:23:47 +01:00
if (Test-Path "$TargetPath" -pathType leaf) {
2023-09-16 12:22:47 +02:00
Write-Host "⏳ Skipping existing $targetDir\$year\$monthDir\$filename..."
2024-08-13 14:04:34 +02:00
return 1
2021-07-15 19:54:00 +02:00
}
2024-08-13 14:04:34 +02:00
Write-Host "⏳ Copying $filename to $targetDir\$year\$monthDir\..."
New-Item -path "$targetDir" -name "$year" -itemType "directory" -force | out-null
New-Item -path "$targetDir/$year" -name "$monthDir" -itemType "directory" -force | out-null
Copy-Item "$sourcePath" "$TargetPath" -force
return 0
2021-07-15 17:45:59 +02:00
}
try {
2023-09-16 12:22:47 +02:00
if ($sourceDir -eq "") { $sourceDir = Read-Host "Enter file path to the source directory" }
if ($targetDir -eq "") { $targetDir = Read-Host "Enter file path to the target directory" }
$stopWatch = [system.diagnostics.stopWatch]::startNew()
2023-01-07 17:23:47 +01:00
2023-09-16 12:22:47 +02:00
Write-Host "⏳ Checking source directory 📂$($sourceDir)..."
if (-not(Test-Path "$sourceDir" -pathType container)) { throw "Can't access source directory: $sourceDir" }
$files = (Get-ChildItem "$sourceDir\*.jpg" -attributes !Directory)
2023-01-07 17:23:47 +01:00
2023-09-16 12:22:47 +02:00
Write-Host "⏳ Checking target directory 📂$($targetDir)..."
if (-not(Test-Path "$targetDir" -pathType container)) { throw "Can't access target directory: $targetDir" }
2023-01-07 17:23:47 +01:00
2024-08-13 14:04:34 +02:00
[int]$skipped = 0
2023-09-16 12:22:47 +02:00
foreach($file in $files) {
$filename = (Get-Item "$file").Name
if ("$filename" -like "IMG_*_*.jpg") {
2024-08-13 14:04:34 +02:00
$array = $filename.split("_")
$skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
2023-09-16 12:22:47 +02:00
} elseif ("$filename" -like "IMG-*-*.jpg") {
2024-08-13 14:04:34 +02:00
$array = $filename.split("-")
$skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
2023-09-16 12:22:47 +02:00
} elseif ("$filename" -like "PANO_*_*.jpg") {
2024-08-13 14:04:34 +02:00
$array = $filename.split("_")
$skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
2023-09-16 12:22:47 +02:00
} elseif ("$filename" -like "PANO-*-*.jpg") {
2024-08-13 14:04:34 +02:00
$array = $filename.split("-")
$skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
2023-09-16 12:22:47 +02:00
} elseif ("$filename" -like "SAVE_*_*.jpg") {
2024-08-13 14:04:34 +02:00
$array = $filename.split("_")
$skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
2024-01-27 12:19:35 +01:00
} elseif ("$filename" -like "PXL_*_*.jpg") {
2024-08-13 14:04:34 +02:00
$array = $filename.split("_")
$skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
2021-07-15 17:45:59 +02:00
} else {
2023-09-16 12:22:47 +02:00
Write-Host "⏳ Skipping $filename with unknown filename format..."
2024-08-13 14:04:34 +02:00
$skipped++
2021-07-15 17:45:59 +02:00
}
}
2023-09-16 12:22:47 +02:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-08-13 14:04:34 +02:00
[int]$copied = $files.Count - $skipped
2024-10-01 13:37:53 +02:00
"✅ Copied $copied photos ($skipped skipped) to 📂$targetDir in $($elapsed)s."
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-07-15 17:45:59 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-07-15 17:45:59 +02:00
exit 1
2023-08-06 21:35:36 +02:00
}