Add copy-photos-sorted.ps1

This commit is contained in:
Markus Fleschutz 2021-07-15 17:45:59 +02:00
parent b97098fc42
commit eebb711087
3 changed files with 69 additions and 1 deletions

View File

@ -47,6 +47,7 @@ close-system-settings.ps1, closes the System Settings gracefully
close-thunderbird.ps1, closes Mozilla Thunderbird gracefully
close-vlc.ps1, closes the VLC media player gracefully
close-windows-terminal.ps1, closes Windows Terminal gracefully
copy-photos-sorted.ps1, Copies all photos sorted by year and month
configure-git.ps1, sets up the Git user configuration
convert-csv2txt.ps1, converts the given CSV file into a text list
convert-mysql2csv.ps1, converts the MySQL database table to a CSV file

1 Script Description
47 close-thunderbird.ps1 closes Mozilla Thunderbird gracefully
48 close-vlc.ps1 closes the VLC media player gracefully
49 close-windows-terminal.ps1 closes Windows Terminal gracefully
50 copy-photos-sorted.ps1 Copies all photos sorted by year and month
51 configure-git.ps1 sets up the Git user configuration
52 convert-csv2txt.ps1 converts the given CSV file into a text list
53 convert-mysql2csv.ps1 converts the MySQL database table to a CSV file

View File

@ -92,7 +92,6 @@ Mega Collection of PowerShell Scripts
📁 Scripts for Files & Folders
-------------------------------
* [clear-recycle-bin.ps1](Scripts/clear-recycle-bin.ps1) - removes the content of the recycle bin folder (can not be undo!)
* [cd-desktop.ps1](Scripts/cd-desktop.ps1) - go to the user's desktop folder
* [cd-docs.ps1](Scripts/cd-docs.ps1) - go to the user's documents folder
* [cd-downloads.ps1](Scripts/cd-downloads.ps1) - go to the user's downloads folder
@ -112,6 +111,8 @@ Mega Collection of PowerShell Scripts
* [cd-videos.ps1](Scripts/cd-videos.ps1) - go to the user's videos folder
* [check-symlinks.ps1](Scripts/check-symlinks.ps1) - checks every symlink in the given directory tree
* [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity
* [clear-recycle-bin.ps1](Scripts/clear-recycle-bin.ps1) - removes the content of the recycle bin folder (can not be undo!)
* [copy-photos-sorted.ps1](Scripts/copy-photos-sorted.ps1) - Copies all photos sorted by year and month
* [create-shortcut.ps1](Scripts/create-shortcut.ps1) - creates a shortcut
* [create-symlink.ps1](Scripts/create-symlink.ps1) - creates a symbolic link
* [decrypt-file.ps1](Scripts/decrypt-file.ps1) - encrypts the given file

66
Scripts/copy-photos-sorted.ps1 Executable file
View File

@ -0,0 +1,66 @@
<#
.SYNOPSIS
copy-photos-sorted.ps1 [<source-dir>] [<target-dir-tree>]
.DESCRIPTION
Copies all photos in source-dir sorted by year and month into the target-dir-tree
.EXAMPLE
PS> .\copy-photos-sorted.ps1 C:\MyPhotos C:\MyPhotoAlbum
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz / License: CC0
#>
param([string]$SourceDir = "", [string]$TargetDirTree = "")
function CopyFile { param([string]$File, [string]$Filename, [int]$Date, [string]$TargetDir)
[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"}
}
$TargetFile = "$TargetDirTree/$Year/$MonthDir/$Filename"
"Copying to $TargetFile ..."
copy-item "$File" "$TargetFile" -force
}
try {
if ($SourceDir -eq "") { $SourceDir = read-host "Enter source directory" }
if ($TargetDirTree -eq "") { $TargetDirTree = read-host "Enter target directory tree" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$Files = (get-childItem "$SourceDir\*.jpg" -attributes !Directory)
[int]$Count = 0
foreach ($File in $Files) {
$Filename = (get-item "$File").Name
if ("$Filename" -like "IMG_*_*.jpg") {
$Array = $Filename.split("_")
CopyFile $File $Filename $Array[1] $TargetDirTree
$Count++
} elseif ("$Filename" -like "PANO_*_*.jpg") {
$Array = $Filename.split("_")
CopyFile $Filen $Filename $Array[1] $TargetDirTree
$Count++
} else {
"Unknown filename format: $Filename"
}
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ copied $Count photos sorted into 📂$TargetDirTree in $Elapsed sec"
exit 0
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}