Improve copy-photos-sorted.ps1

This commit is contained in:
Markus Fleschutz 2021-07-15 19:54:00 +02:00
parent f3f8024a6f
commit f10d9962c3

View File

@ -13,7 +13,7 @@
param([string]$SourceDir = "", [string]$TargetDirTree = "") param([string]$SourceDir = "", [string]$TargetDirTree = "")
function CopyFile { param([string]$File, [string]$Filename, [int]$Date, [string]$TargetDir) function CopyFile { param([string]$SrcPath, [string]$Filename, [int]$Date, [string]$DstDir)
[int]$Year = $Date / 10000 [int]$Year = $Date / 10000
[int]$Month = ($Date / 100) % 100 [int]$Month = ($Date / 100) % 100
$MonthDir = switch($Month) { $MonthDir = switch($Month) {
@ -30,9 +30,15 @@ function CopyFile { param([string]$File, [string]$Filename, [int]$Date, [string]
11 {"11 NOV"} 11 {"11 NOV"}
12 {"12 DEC"} 12 {"12 DEC"}
} }
$TargetFile = "$TargetDirTree/$Year/$MonthDir/$Filename" $DstPath = "$DstDir/$Year/$MonthDir/$Filename"
"Copying to $TargetFile ..." if (test-path "$DstPath" -pathType leaf) {
copy-item "$File" "$TargetFile" -force "$DstPath exists, skipping..."
} else {
"Copying to $DstPath ..."
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
}
} }
try { try {
@ -66,7 +72,7 @@ try {
} }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ copied $Count photos sorted into 📂$TargetDirTree in $Elapsed sec" "✔️ copied $Count photos sorted by year and month into 📂$TargetDirTree in $Elapsed sec"
exit 0 exit 0
} catch { } catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"