mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-01-31 09:59:17 +01:00
Update copy-photos-sorted.ps1
This commit is contained in:
parent
6cfc88cf9b
commit
5d74055659
@ -8,7 +8,7 @@
|
|||||||
.PARAMTER TargetDir
|
.PARAMTER TargetDir
|
||||||
Specifies the path to the target folder
|
Specifies the path to the target folder
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./copy-photos-sorted D:\MyPhone\DCIM C:\MyPhotos
|
PS> ./copy-photos-sorted D:\iPhone\DCIM C:\MyPhotos
|
||||||
.LINK
|
.LINK
|
||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
.NOTES
|
.NOTES
|
||||||
@ -35,24 +35,29 @@ function CopyFile { param([string]$SourcePath, [string]$TargetDir, [int]$Date, [
|
|||||||
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) {
|
||||||
"⏳ Skipping $($Filename): already existing..."
|
"⏳ Skipping existing $($Filename)..."
|
||||||
} else {
|
} else {
|
||||||
"⏳ Copying $Filename to $Year/$MonthDir..."
|
"⏳ Copying $Filename to $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 path to source directory" }
|
if ($SourceDir -eq "") { $SourceDir = Read-Host "Enter file path to source directory" }
|
||||||
if ($TargetDir -eq "") { $TargetDir = read-host "Enter path to target directory" }
|
if ($TargetDir -eq "") { $TargetDir = Read-Host "Enter file path to target directory" }
|
||||||
|
|
||||||
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
||||||
|
|
||||||
|
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)
|
$Files = (Get-ChildItem "$SourceDir\*.jpg" -attributes !Directory)
|
||||||
"Found $($Files.Count) photos in 📂$SourceDir..."
|
|
||||||
foreach ($File in $Files) {
|
Write-Host "⏳ Checking target directory 📂$($TargetDir)..."
|
||||||
|
if (-not(Test-Path "$TargetDir" -pathType container)) { throw "Can't access target directory: $TargetDir" }
|
||||||
|
|
||||||
|
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("_")
|
||||||
@ -70,13 +75,13 @@ try {
|
|||||||
$Array = $Filename.split("_")
|
$Array = $Filename.split("_")
|
||||||
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
|
CopyFile "$File" "$TargetDir" $Array[1] "$Filename"
|
||||||
} else {
|
} else {
|
||||||
"⏳ Skipping $($Filename): unknown filename format..."
|
"⏳ Skipping $Filename with unknown filename format..."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
||||||
"✔️ $($Files.Count) photos copied from 📂$SourceDir to 📂$TargetDir in $Elapsed sec"
|
"✔️ copied $($Files.Count) photos from 📂$SourceDir 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])"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user