PowerShell/Scripts/copy-photos-sorted.ps1

83 lines
2.6 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-15 17:45:59 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Copy image files sorted by year and month
2021-07-15 17:45:59 +02:00
.DESCRIPTION
2021-10-16 16:50:10 +02:00
This script copies image files from SourceDir to TargetDir sorted by year and month.
.PARAMETER SourceDir
Specifies the path to the image files
.PARAMTER TargetDir
Specifies the path to the target folder
2021-07-15 17:45:59 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./copy-photos-sorted D:\Mobile\DCIM 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
#>
2021-09-01 13:10:54 +02:00
param([string]$SourceDir = "", [string]$TargetDir = "")
2021-07-15 17:45:59 +02:00
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 {
2021-09-01 13:10:54 +02:00
if ($SourceDir -eq "") { $SourceDir = read-host "Enter path to source directory" }
if ($TargetDir -eq "") { $TargetDir = read-host "Enter path to target directory" }
2021-07-15 17:45:59 +02:00
$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-09-01 13:10:54 +02:00
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
2021-07-15 17:58:15 +02:00
} elseif ("$Filename" -like "IMG-*-*.jpg") {
$Array = $Filename.split("-")
2021-09-01 13:10:54 +02:00
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
2021-07-15 17:45:59 +02:00
} elseif ("$Filename" -like "PANO_*_*.jpg") {
$Array = $Filename.split("_")
2021-09-01 13:10:54 +02:00
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
2021-07-15 17:58:15 +02:00
} elseif ("$Filename" -like "PANO-*-*.jpg") {
$Array = $Filename.split("-")
2021-09-01 13:10:54 +02:00
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
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-09-01 13:10:54 +02:00
"✔️ copied $Num photos to 📂$TargetDir sorted by year and month in $Elapsed sec"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-07-15 17:45:59 +02:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-07-15 17:45:59 +02:00
exit 1
}