mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-06-16 01:46:38 +02:00
Improve copy-photos-sorted.ps1
This commit is contained in:
parent
f10d9962c3
commit
e6d93b5c08
@ -1,4 +1,4 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
copy-photos-sorted.ps1 [<source-dir>] [<target-dir-tree>]
|
copy-photos-sorted.ps1 [<source-dir>] [<target-dir-tree>]
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
param([string]$SourceDir = "", [string]$TargetDirTree = "")
|
param([string]$SourceDir = "", [string]$TargetDirTree = "")
|
||||||
|
|
||||||
function CopyFile { param([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
|
||||||
[int]$Month = ($Date / 100) % 100
|
[int]$Month = ($Date / 100) % 100
|
||||||
$MonthDir = switch($Month) {
|
$MonthDir = switch($Month) {
|
||||||
@ -32,9 +32,9 @@ function CopyFile { param([string]$SrcPath, [string]$Filename, [int]$Date, [stri
|
|||||||
}
|
}
|
||||||
$DstPath = "$DstDir/$Year/$MonthDir/$Filename"
|
$DstPath = "$DstDir/$Year/$MonthDir/$Filename"
|
||||||
if (test-path "$DstPath" -pathType leaf) {
|
if (test-path "$DstPath" -pathType leaf) {
|
||||||
"$DstPath exists, skipping..."
|
"#$($Num): $Filename exists already, skipping..."
|
||||||
} else {
|
} else {
|
||||||
"Copying to $DstPath ..."
|
"#$($Num): $Filename is copied..."
|
||||||
new-item -path "$DstDir" -name "$Year" -itemType "directory" -force | out-null
|
new-item -path "$DstDir" -name "$Year" -itemType "directory" -force | out-null
|
||||||
new-item -path "$DstDir/$Year" -name "$MonthDir" -itemType "directory" -force | out-null
|
new-item -path "$DstDir/$Year" -name "$MonthDir" -itemType "directory" -force | out-null
|
||||||
copy-item "$SrcPath" "$DstPath" -force
|
copy-item "$SrcPath" "$DstPath" -force
|
||||||
@ -47,32 +47,30 @@ try {
|
|||||||
|
|
||||||
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
||||||
$Files = (get-childItem "$SourceDir\*.jpg" -attributes !Directory)
|
$Files = (get-childItem "$SourceDir\*.jpg" -attributes !Directory)
|
||||||
[int]$Count = 0
|
"Found $($Files.Count) photos in 📂$SourceDir..."
|
||||||
|
[int]$Num = 0
|
||||||
foreach ($File in $Files) {
|
foreach ($File in $Files) {
|
||||||
|
$Num++
|
||||||
$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 $File $Filename $Array[1] $TargetDirTree
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
||||||
$Count++
|
|
||||||
} elseif ("$Filename" -like "IMG-*-*.jpg") {
|
} elseif ("$Filename" -like "IMG-*-*.jpg") {
|
||||||
$Array = $Filename.split("-")
|
$Array = $Filename.split("-")
|
||||||
CopyFile $File $Filename $Array[1] $TargetDirTree
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
||||||
$Count++
|
|
||||||
} elseif ("$Filename" -like "PANO_*_*.jpg") {
|
} elseif ("$Filename" -like "PANO_*_*.jpg") {
|
||||||
$Array = $Filename.split("_")
|
$Array = $Filename.split("_")
|
||||||
CopyFile $Filen $Filename $Array[1] $TargetDirTree
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
||||||
$Count++
|
|
||||||
} elseif ("$Filename" -like "PANO-*-*.jpg") {
|
} elseif ("$Filename" -like "PANO-*-*.jpg") {
|
||||||
$Array = $Filename.split("-")
|
$Array = $Filename.split("-")
|
||||||
CopyFile $Filen $Filename $Array[1] $TargetDirTree
|
CopyFile $Num "$File" "$Filename" $Array[1] "$TargetDirTree"
|
||||||
$Count++
|
|
||||||
} else {
|
} else {
|
||||||
"Can't extract year and month from: $Filename"
|
"#$($Num): $Filename with unknown filename format"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
||||||
"✔️ copied $Count photos sorted by year and month into 📂$TargetDirTree in $Elapsed sec"
|
"✔️ copied $Num photos sorted by year and month to 📂$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])"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user