PowerShell/Scripts/copy-photos-sorted.ps1

88 lines
3.0 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +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
2022-03-28 13:06:32 +02:00
This PowerShell script copies image files from SourceDir to TargetDir sorted by year and month.
2021-10-16 16:50:10 +02:00
.PARAMETER SourceDir
2022-03-28 13:06:32 +02:00
Specifies the path to the source folder
2021-10-16 16:50:10 +02:00
.PARAMTER TargetDir
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
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
#>
2021-09-01 13:10:54 +02:00
param([string]$SourceDir = "", [string]$TargetDir = "")
2021-07-15 17:45:59 +02:00
2022-01-08 15:01:27 +01:00
function CopyFile { param([string]$SourcePath, [string]$TargetDir, [int]$Date, [string]$Filename)
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"}
}
2022-01-08 15:01:27 +01:00
$TargetPath = "$TargetDir/$Year/$MonthDir/$Filename"
2023-01-07 17:23:47 +01:00
if (Test-Path "$TargetPath" -pathType leaf) {
2023-01-07 18:10:31 +01:00
Write-Host "⏳ Skipping existing $Year/$MonthDir/$Filename..."
2021-07-15 19:54:00 +02:00
} else {
2023-01-07 18:10:31 +01:00
Write-Host "⏳ Copying $Filename to $Year/$MonthDir..."
2023-01-07 17:23:47 +01:00
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
2021-07-15 19:54:00 +02:00
}
2021-07-15 17:45:59 +02:00
}
try {
2023-01-07 17:23:47 +01:00
if ($SourceDir -eq "") { $SourceDir = Read-Host "Enter file path to source directory" }
if ($TargetDir -eq "") { $TargetDir = Read-Host "Enter file path to target directory" }
2021-07-15 17:45:59 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-01-07 17:23:47 +01:00
Write-Host "⏳ Checking source directory 📂$($SourceDir)..."
if (-not(Test-Path "$SourceDir" -pathType container)) { throw "Can't access source directory: $SourceDir" }
2022-01-08 15:01:27 +01:00
$Files = (Get-ChildItem "$SourceDir\*.jpg" -attributes !Directory)
2023-01-07 17:23:47 +01:00
Write-Host "⏳ Checking target directory 📂$($TargetDir)..."
if (-not(Test-Path "$TargetDir" -pathType container)) { throw "Can't access target directory: $TargetDir" }
foreach($File in $Files) {
2022-01-08 15:01:27 +01:00
$Filename = (Get-Item "$File").Name
2021-07-15 17:45:59 +02:00
if ("$Filename" -like "IMG_*_*.jpg") {
$Array = $Filename.split("_")
2022-01-08 15:01:27 +01:00
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
2021-07-15 17:58:15 +02:00
} elseif ("$Filename" -like "IMG-*-*.jpg") {
$Array = $Filename.split("-")
2022-01-08 15:01:27 +01:00
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
2021-07-15 17:45:59 +02:00
} elseif ("$Filename" -like "PANO_*_*.jpg") {
$Array = $Filename.split("_")
2022-01-08 15:01:27 +01:00
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
2021-07-15 17:58:15 +02:00
} elseif ("$Filename" -like "PANO-*-*.jpg") {
$Array = $Filename.split("-")
2022-01-08 15:01:27 +01:00
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
2022-01-08 13:58:54 +01:00
} elseif ("$Filename" -like "SAVE_*_*.jpg") {
$Array = $Filename.split("_")
2022-01-08 15:01:27 +01:00
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
2021-07-15 17:45:59 +02:00
} else {
2023-01-07 18:10:31 +01:00
Write-Host "⏳ Skipping $Filename with unknown filename format..."
2021-07-15 17:45:59 +02:00
}
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-01-07 17:23:47 +01:00
"✔️ copied $($Files.Count) photos from 📂$SourceDir to 📂$TargetDir in $Elapsed sec"
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
}