Improve copy-photos-sorted.ps1

This commit is contained in:
Markus Fleschutz 2021-09-01 13:10:54 +02:00
parent 0149a7e5a6
commit 14d7a4b137

View File

@ -1,17 +1,17 @@
<# <#
.SYNOPSIS .SYNOPSIS
copy-photos-sorted.ps1 [<source-dir>] [<target-dir-tree>] copy-photos-sorted.ps1 [<SourceDir>] [<TargetDir>]
.DESCRIPTION .DESCRIPTION
Copies all photos in source-dir sorted by year and month into the target-dir-tree. Copies all photos in SourceDir to TargetDir sorted by year and month.
.EXAMPLE .EXAMPLE
PS> .\copy-photos-sorted.ps1 C:\MyPhotos C:\MyPhotoAlbum PS> .\copy-photos-sorted.ps1 D:\Mobile\DCIM C:\MyPhotoAlbum
.NOTES .NOTES
Author: Markus Fleschutz · License: CC0 Author: Markus Fleschutz · License: CC0
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
#> #>
param([string]$SourceDir = "", [string]$TargetDirTree = "") param([string]$SourceDir = "", [string]$TargetDir = "")
function CopyFile { param([int]$Num, [string]$SrcPath, [string]$Filename, [int]$Date, [string]$DstDir) function CopyFile { param([int]$Num, [string]$SrcPath, [string]$Filename, [int]$Date, [string]$DstDir)
[int]$Year = $Date / 10000 [int]$Year = $Date / 10000
@ -42,8 +42,8 @@ function CopyFile { param([int]$Num, [string]$SrcPath, [string]$Filename, [int]$
} }
try { try {
if ($SourceDir -eq "") { $SourceDir = read-host "Enter source directory" } if ($SourceDir -eq "") { $SourceDir = read-host "Enter path to source directory" }
if ($TargetDirTree -eq "") { $TargetDirTree = read-host "Enter target directory tree" } if ($TargetDir -eq "") { $TargetDir = read-host "Enter path to target directory" }
$StopWatch = [system.diagnostics.stopwatch]::startNew() $StopWatch = [system.diagnostics.stopwatch]::startNew()
$Files = (get-childItem "$SourceDir\*.jpg" -attributes !Directory) $Files = (get-childItem "$SourceDir\*.jpg" -attributes !Directory)
@ -54,23 +54,23 @@ try {
$Filename = (get-item "$File").Name $Filename = (get-item "$File").Name
if ("$Filename" -like "IMG_*_*.jpg") { if ("$Filename" -like "IMG_*_*.jpg") {
$Array = $Filename.split("_") $Array = $Filename.split("_")
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree" CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
} elseif ("$Filename" -like "IMG-*-*.jpg") { } elseif ("$Filename" -like "IMG-*-*.jpg") {
$Array = $Filename.split("-") $Array = $Filename.split("-")
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree" CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
} elseif ("$Filename" -like "PANO_*_*.jpg") { } elseif ("$Filename" -like "PANO_*_*.jpg") {
$Array = $Filename.split("_") $Array = $Filename.split("_")
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree" CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
} elseif ("$Filename" -like "PANO-*-*.jpg") { } elseif ("$Filename" -like "PANO-*-*.jpg") {
$Array = $Filename.split("-") $Array = $Filename.split("-")
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree" CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDir"
} else { } else {
"#$($Num): $Filename with unknown filename format" "#$($Num): $Filename with unknown filename format"
} }
} }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ copied $Num photos sorted by year and month to 📂$TargetDirTree in $Elapsed sec" "✔️ copied $Num photos to 📂$TargetDir sorted by year and month 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])"