PowerShell/Docs/copy-photos-sorted.md

110 lines
3.5 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*copy-photos-sorted.ps1*
================
2022-11-17 20:02:26 +01:00
copy-photos-sorted.ps1 [[-SourceDir] <string>] [[-TargetDir] <string>]
2022-11-17 19:46:02 +01:00
2021-10-17 14:33:27 +02:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-10-17 14:33:27 +02:00
```powershell
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Copy photos sorted by year and month
.DESCRIPTION
This PowerShell script copies image files from SourceDir to TargetDir sorted by year and month.
.PARAMETER SourceDir
Specifies the path to the source folder
.PARAMTER TargetDir
Specifies the path to the target folder
.EXAMPLE
2023-05-26 12:20:18 +02:00
PS> ./copy-photos-sorted D:\iPhone\DCIM C:\MyPhotos
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$SourceDir = "", [string]$TargetDir = "")
function CopyFile { param([string]$SourcePath, [string]$TargetDir, [int]$Date, [string]$Filename)
[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"}
}
$TargetPath = "$TargetDir/$Year/$MonthDir/$Filename"
2023-05-26 12:20:18 +02:00
if (Test-Path "$TargetPath" -pathType leaf) {
Write-Host "⏳ Skipping existing $Year/$MonthDir/$Filename..."
2022-11-17 20:02:26 +01:00
} else {
2023-05-26 12:20:18 +02:00
Write-Host "⏳ Copying $Filename to $Year/$MonthDir..."
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
2022-11-17 20:02:26 +01:00
}
}
try {
2023-05-26 12:20:18 +02: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" }
2022-11-17 20:02:26 +01:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-05-26 12:20:18 +02:00
Write-Host "⏳ Checking source directory 📂$($SourceDir)..."
if (-not(Test-Path "$SourceDir" -pathType container)) { throw "Can't access source directory: $SourceDir" }
2022-11-17 20:02:26 +01:00
$Files = (Get-ChildItem "$SourceDir\*.jpg" -attributes !Directory)
2023-05-26 12:20:18 +02: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-11-17 20:02:26 +01:00
$Filename = (Get-Item "$File").Name
if ("$Filename" -like "IMG_*_*.jpg") {
$Array = $Filename.split("_")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
} elseif ("$Filename" -like "IMG-*-*.jpg") {
$Array = $Filename.split("-")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
} elseif ("$Filename" -like "PANO_*_*.jpg") {
$Array = $Filename.split("_")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
} elseif ("$Filename" -like "PANO-*-*.jpg") {
$Array = $Filename.split("-")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
} elseif ("$Filename" -like "SAVE_*_*.jpg") {
$Array = $Filename.split("_")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
} else {
2023-05-26 12:20:18 +02:00
Write-Host "⏳ Skipping $Filename with unknown filename format..."
2022-11-17 20:02:26 +01:00
}
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-05-26 12:20:18 +02:00
"✔️ copied $($Files.Count) photos from 📂$SourceDir to 📂$TargetDir in $Elapsed sec"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-08-06 11:42:46 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of copy-photos-sorted.ps1 as of 08/06/2023 11:42:27)*