Updated some files

This commit is contained in:
Markus Fleschutz 2024-08-13 14:04:34 +02:00
parent 80702d4874
commit d1174a0924
2 changed files with 33 additions and 31 deletions

View File

@ -2,15 +2,15 @@
.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\... Copying IMG_20240903_134445.jpg to C:\MyPhotos\2024\09 SEP\...
Copied 1 photo to 📂C:\MyPhotos in 41 sec Copied 1 photo (0 skipped) to 📂C:\MyPhotos in 41s.
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -39,12 +39,13 @@ function CopyFile { param([string]$sourcePath, [string]$targetDir, [int]$date, [
$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 $targetDir\$year\$monthDir\$filename..." Write-Host "⏳ Skipping existing $targetDir\$year\$monthDir\$filename..."
} else { return 1
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
} }
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
return 0
} }
try { try {
@ -59,32 +60,35 @@ try {
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" }
[int]$skipped = 0
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" $skipped += 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" $skipped += 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" $skipped += 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" $skipped += 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" $skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
} elseif ("$filename" -like "PXL_*_*.jpg") { } elseif ("$filename" -like "PXL_*_*.jpg") {
$Array = $filename.split("_") $array = $filename.split("_")
CopyFile "$file" "$targetDir" $Array[1] "$filename" $skipped += CopyFile "$file" "$targetDir" $array[1] "$filename"
} else { } else {
Write-Host "⏳ Skipping $filename with unknown filename format..." Write-Host "⏳ Skipping $filename with unknown filename format..."
$skipped++
} }
} }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Copied $($files.Count) photos to 📂$targetDir in $elapsed sec" [int]$copied = $files.Count - $skipped
"✔️ Copied $copied photos ($skipped skipped) to 📂$targetDir in $($elapsed)s."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -1,8 +1,8 @@
<# <#
.SYNOPSIS .SYNOPSIS
Searches for a text pattern in files Searches for text in files
.DESCRIPTION .DESCRIPTION
This PowerShell script searches for the given pattern in the given files. This PowerShell script searches for the given text pattern in the given files.
.PARAMETER textPattern .PARAMETER textPattern
Specifies the text pattern to search for Specifies the text pattern to search for
.PARAMETER filePattern .PARAMETER filePattern
@ -22,17 +22,15 @@
param([string]$textPattern = "", [string]$filePattern = "") param([string]$textPattern = "", [string]$filePattern = "")
function ListLocations { param([string]$Pattern, [string]$Path) function ListLocations { param([string]$textPattern, [string]$filePattern)
$list = Select-String -path $Path -pattern "$Pattern" $list = Select-String -path $filePattern -pattern "$textPattern"
foreach ($item in $list) { foreach($item in $list) { New-Object PSObject -Property @{ 'FILE'="$($item.Path)"; 'LINE'="$($item.LineNumber):$($item.Line)" } }
New-Object PSObject -Property @{ 'FILE'="$($item.Path)"; 'LINE'="$($item.LineNumber):$($item.Line)" } "✔️ Found $($list.Count) lines containing '$textPattern' in $filePattern."
}
Write-Output "✔️ Found $($list.Count) lines containing '$Pattern' in $filePattern."
} }
try { try {
if ($textPattern -eq "" ) { $textPattern = Read-Host "Enter the text pattern (e.g. 'UFO')" } if ($textPattern -eq "" ) { $textPattern = Read-Host "Enter the text pattern, e.g. 'UFO'" }
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern (e.g. '*.ps1')" } if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern, e.g. '*.ps1'" }
ListLocations $textPattern $filePattern | Format-Table -property FILE,LINE -autoSize ListLocations $textPattern $filePattern | Format-Table -property FILE,LINE -autoSize
exit 0 # success exit 0 # success