2024-01-25 13:37:12 +01:00
|
|
|
Script: *copy-photos-sorted.ps1*
|
|
|
|
========================
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-09-20 17:05:11 +02: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
|
2023-09-20 17:05:11 +02:00
|
|
|
This PowerShell script copies image files from sourceDir to targetDir sorted by year and month.
|
|
|
|
.PARAMETER sourceDir
|
2022-11-17 20:02:26 +01:00
|
|
|
Specifies the path to the source folder
|
2023-09-20 17:05:11 +02:00
|
|
|
.PARAMTER targetDir
|
2022-11-17 20:02:26 +01:00
|
|
|
Specifies the path to the target folder
|
|
|
|
.EXAMPLE
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./copy-photos-sorted.ps1 D:\iPhone\DCIM C:\MyPhotos
|
2023-09-20 17:05:11 +02:00
|
|
|
⏳ Copying IMG_20230903_134445.jpg to C:\MyPhotos\2023\09 SEP\...
|
|
|
|
✔️ Copied 1 photo to 📂C:\MyPhotos in 41 sec
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2023-09-20 17:05:11 +02:00
|
|
|
param([string]$sourceDir = "", [string]$targetDir = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-09-20 17:05:11 +02:00
|
|
|
function CopyFile { param([string]$sourcePath, [string]$targetDir, [int]$date, [string]$filename)
|
|
|
|
[int]$year = $date / 10000
|
|
|
|
[int]$month = ($date / 100) % 100
|
|
|
|
$monthDir = switch($month) {
|
2022-11-17 20:02:26 +01:00
|
|
|
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"}
|
|
|
|
}
|
2023-09-20 17:05:11 +02:00
|
|
|
$TargetPath = "$targetDir/$year/$monthDir/$filename"
|
2023-05-26 12:20:18 +02:00
|
|
|
if (Test-Path "$TargetPath" -pathType leaf) {
|
2023-09-20 17:05:11 +02:00
|
|
|
Write-Host "⏳ Skipping existing $targetDir\$year\$monthDir\$filename..."
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2023-09-20 17:05:11 +02:00
|
|
|
Write-Host "⏳ Copying $filename to $targetDir\$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-09-20 17:05:11 +02:00
|
|
|
if ($sourceDir -eq "") { $sourceDir = Read-Host "Enter file path to the source directory" }
|
|
|
|
if ($targetDir -eq "") { $targetDir = Read-Host "Enter file path to the target directory" }
|
|
|
|
$stopWatch = [system.diagnostics.stopWatch]::startNew()
|
2023-05-26 12:20:18 +02:00
|
|
|
|
2023-09-20 17:05:11 +02:00
|
|
|
Write-Host "⏳ Checking source directory 📂$($sourceDir)..."
|
|
|
|
if (-not(Test-Path "$sourceDir" -pathType container)) { throw "Can't access source directory: $sourceDir" }
|
|
|
|
$files = (Get-ChildItem "$sourceDir\*.jpg" -attributes !Directory)
|
2023-05-26 12:20:18 +02:00
|
|
|
|
2023-09-20 17:05:11 +02:00
|
|
|
Write-Host "⏳ Checking target directory 📂$($targetDir)..."
|
|
|
|
if (-not(Test-Path "$targetDir" -pathType container)) { throw "Can't access target directory: $targetDir" }
|
2023-05-26 12:20:18 +02:00
|
|
|
|
2023-09-20 17:05:11 +02:00
|
|
|
foreach($file in $files) {
|
|
|
|
$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"
|
2024-03-27 17:36:59 +01:00
|
|
|
} elseif ("$filename" -like "PXL_*_*.jpg") {
|
|
|
|
$Array = $filename.split("_")
|
|
|
|
CopyFile "$file" "$targetDir" $Array[1] "$filename"
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2023-09-20 17:05:11 +02:00
|
|
|
Write-Host "⏳ Skipping $filename with unknown filename format..."
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-20 17:05:11 +02:00
|
|
|
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
|
|
|
"✔️ Copied $($files.Count) photos 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
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of copy-photos-sorted.ps1 as of 05/19/2024 10:25:19)*
|