This commit is contained in:
Markus Fleschutz 2023-09-17 11:31:26 +02:00
commit b2cb346fa6
2 changed files with 47 additions and 45 deletions

View File

@ -5,7 +5,7 @@
This PowerShell script queries the PowerShell status and prints it. This PowerShell script queries the PowerShell status and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-powershell.ps1 PS> ./check-powershell.ps1
PowerShell 5.1.19041.2673 (Desktop edition, 10 modules, 1458 cmdlets, 172 aliases) PowerShell 5.1.19041.2673 Desktop edition (10 modules, 1458 cmdlets, 172 aliases)
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -18,10 +18,10 @@ try {
$numModules = (Get-Module).Count $numModules = (Get-Module).Count
$numAliases = (Get-Alias).Count $numAliases = (Get-Alias).Count
if ($IsLinux) { if ($IsLinux) {
"✅ PowerShell $version ($edition edition, $numModules modules, $numAliases aliases)" "✅ PowerShell $version $edition edition ($numModules modules, $numAliases aliases)"
} else { } else {
$numCmdlets = (Get-Command -Command-Type cmdlet).Count $numCmdlets = (Get-Command -Command-Type cmdlet).Count
"✅ PowerShell $version ($edition edition, $numModules modules, $numCmdlets cmdlets, $numAliases aliases)" "✅ PowerShell $version $edition edition ($numModules modules, $numCmdlets cmdlets, $numAliases aliases)"
} }
exit 0 # success exit 0 # success
} catch { } catch {

View File

@ -2,25 +2,27 @@
.SYNOPSIS .SYNOPSIS
Copy photos sorted by year and month Copy photos sorted by year and month
.DESCRIPTION .DESCRIPTION
This PowerShell script copies image files from SourceDir to TargetDir sorted by year and month. This PowerShell script copies image files from sourceDir to targetDir sorted by year and month.
.PARAMETER SourceDir .PARAMETER sourceDir
Specifies the path to the source folder Specifies the path to the source folder
.PARAMTER TargetDir .PARAMTER targetDir
Specifies the path to the target folder Specifies the path to the target folder
.EXAMPLE .EXAMPLE
PS> ./copy-photos-sorted.ps1 D:\iPhone\DCIM C:\MyPhotos PS> ./copy-photos-sorted.ps1 D:\iPhone\DCIM C:\MyPhotos
Copying IMG_20230903_134445.jpg to C:\MyPhotos\2023\09 SEP\...
Copied 1 photo to 📂C:\MyPhotos in 41 sec
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$SourceDir = "", [string]$TargetDir = "") param([string]$sourceDir = "", [string]$targetDir = "")
function CopyFile { param([string]$SourcePath, [string]$TargetDir, [int]$Date, [string]$Filename) function CopyFile { param([string]$sourcePath, [string]$targetDir, [int]$date, [string]$filename)
[int]$Year = $Date / 10000 [int]$year = $date / 10000
[int]$Month = ($Date / 100) % 100 [int]$month = ($date / 100) % 100
$MonthDir = switch($Month) { $monthDir = switch($month) {
1 {"01 JAN"} 1 {"01 JAN"}
2 {"02 FEB"} 2 {"02 FEB"}
3 {"03 MAR"} 3 {"03 MAR"}
@ -34,52 +36,52 @@ function CopyFile { param([string]$SourcePath, [string]$TargetDir, [int]$Date, [
11 {"11 NOV"} 11 {"11 NOV"}
12 {"12 DEC"} 12 {"12 DEC"}
} }
$TargetPath = "$TargetDir/$Year/$MonthDir/$Filename" $TargetPath = "$targetDir/$year/$monthDir/$filename"
if (Test-Path "$TargetPath" -pathType leaf) { if (Test-Path "$TargetPath" -pathType leaf) {
Write-Host "⏳ Skipping existing $Year/$MonthDir/$Filename..." Write-Host "⏳ Skipping existing $targetDir\$year\$monthDir\$filename..."
} else { } else {
Write-Host "⏳ Copying $Filename to $Year/$MonthDir..." Write-Host "⏳ Copying $filename to $targetDir\$year\$monthDir\..."
New-Item -path "$TargetDir" -name "$Year" -itemType "directory" -force | out-null New-Item -path "$targetDir" -name "$year" -itemType "directory" -force | out-null
New-Item -path "$TargetDir/$Year" -name "$MonthDir" -itemType "directory" -force | out-null New-Item -path "$targetDir/$year" -name "$monthDir" -itemType "directory" -force | out-null
Copy-Item "$SourcePath" "$TargetPath" -force Copy-Item "$sourcePath" "$TargetPath" -force
} }
} }
try { try {
if ($SourceDir -eq "") { $SourceDir = Read-Host "Enter file path to source directory" } if ($sourceDir -eq "") { $sourceDir = Read-Host "Enter file path to the source directory" }
if ($TargetDir -eq "") { $TargetDir = Read-Host "Enter file path to target directory" } if ($targetDir -eq "") { $targetDir = Read-Host "Enter file path to the target directory" }
$StopWatch = [system.diagnostics.stopwatch]::startNew() $stopWatch = [system.diagnostics.stopWatch]::startNew()
Write-Host "⏳ Checking source directory 📂$($SourceDir)..." Write-Host "⏳ Checking source directory 📂$($sourceDir)..."
if (-not(Test-Path "$SourceDir" -pathType container)) { throw "Can't access source directory: $SourceDir" } if (-not(Test-Path "$sourceDir" -pathType container)) { throw "Can't access source directory: $sourceDir" }
$Files = (Get-ChildItem "$SourceDir\*.jpg" -attributes !Directory) $files = (Get-ChildItem "$sourceDir\*.jpg" -attributes !Directory)
Write-Host "⏳ Checking target directory 📂$($TargetDir)..." Write-Host "⏳ Checking target directory 📂$($targetDir)..."
if (-not(Test-Path "$TargetDir" -pathType container)) { throw "Can't access target directory: $TargetDir" } if (-not(Test-Path "$targetDir" -pathType container)) { throw "Can't access target directory: $targetDir" }
foreach($File in $Files) { foreach($file in $files) {
$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" "$TargetDir" $Array[1] "$Filename" CopyFile "$file" "$targetDir" $Array[1] "$filename"
} elseif ("$Filename" -like "IMG-*-*.jpg") { } elseif ("$filename" -like "IMG-*-*.jpg") {
$Array = $Filename.split("-") $Array = $filename.split("-")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename" CopyFile "$file" "$targetDir" $Array[1] "$filename"
} elseif ("$Filename" -like "PANO_*_*.jpg") { } elseif ("$filename" -like "PANO_*_*.jpg") {
$Array = $Filename.split("_") $Array = $filename.split("_")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename" CopyFile "$file" "$targetDir" $Array[1] "$filename"
} elseif ("$Filename" -like "PANO-*-*.jpg") { } elseif ("$filename" -like "PANO-*-*.jpg") {
$Array = $Filename.split("-") $Array = $filename.split("-")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename" CopyFile "$file" "$targetDir" $Array[1] "$filename"
} elseif ("$Filename" -like "SAVE_*_*.jpg") { } elseif ("$filename" -like "SAVE_*_*.jpg") {
$Array = $Filename.split("_") $Array = $filename.split("_")
CopyFile "$File" "$TargetDir" $Array[1] "$Filename" CopyFile "$file" "$targetDir" $Array[1] "$filename"
} else { } else {
Write-Host "⏳ Skipping $Filename with unknown filename format..." Write-Host "⏳ Skipping $filename with unknown filename format..."
} }
} }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ copied $($Files.Count) photos from 📂$SourceDir to 📂$TargetDir in $Elapsed sec" "✔️ Copied $($files.Count) photos to 📂$targetDir in $elapsed sec"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"