Created
May 21, 2019 08:17
-
-
Save GeeLaw/652c571b6671d4a633a729988dde1b14 to your computer and use it in GitHub Desktop.
Photo organizer (V2EX 566127)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
& { | |
$source = '~\Documents\Playground\MovePhotos' | |
$destinations = @( | |
'~\Documents\Playground\MovePhotos\Category1', | |
'~\Documents\Playground\MovePhotos\Category2', | |
'~\Documents\Playground\MovePhotos\Category3' | |
) | |
$destinationNames = @( | |
'Category1', | |
'Category2', | |
'Category3' | |
) | |
$photoFilter = @('*.jpg', '*.png') | |
If ($destinations -eq 0) | |
{ | |
Write-Error 'No destination specified.' | |
Return | |
} | |
If ($destinations.Count -gt 10) | |
{ | |
Write-Error 'Cannot handle more than 10 destinations. Are you sane?' | |
Return | |
} | |
If ($destinations.Count -ne $destinationNames.Count) | |
{ | |
Write-Error '#[Destinations] != #[Destination Names]' | |
Return | |
} | |
$choices = [System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]]::new() | |
0..($destinations.Count - 1) | ForEach-Object { | |
$choices.Add([System.Management.Automation.Host.ChoiceDescription]::new( | |
'[&' + (($_ + 1) % 10) + '] ' + $destinationNames[$_], | |
'Move to ' + $destinations[$_] | |
)) | |
} | |
$choices.Add([System.Management.Automation.Host.ChoiceDescription]::new( | |
'Skip', | |
'Do not move this item and continue processing. This is the default.' | |
)) | |
$shell = New-Object -ComObject 'WScript.Shell' | |
$self = (Get-Process -Id $pid).MainWindowTitle | |
$rundllArg = "`"$env:ProgramFiles\Windows Photo Viewer\PhotoViewer.dll`", ImageView_Fullscreen " | |
Get-ChildItem -LiteralPath $source -Include $photoFilter -File | ForEach-Object { | |
<# Open the photo with Windows Photo Previewer. #> | |
$proc = Start-Process 'rundll32.exe' -ArgumentList ($rundllArg + $_.FullName) -PassThru | |
<# Wait until the window is shown, then take back the focus. #> | |
While ($proc.MainWindowHandle -eq 0) { Start-Sleep -Milliseconds 200; } | |
$shell.AppActivate($self) | |
$choice = $Host.UI.PromptForChoice( | |
'Moving this photo', | |
'Where do you want to move this photo? (Press Escape to abort any further processing.)', | |
$choices, | |
$choices.Count - 1 | |
) | |
If ($choice -lt $choices.Count - 1) | |
{ | |
$_ | Move-Item -Destination ($destinations[$choice]) | |
} | |
If (-not $proc.HasExited) | |
{ | |
$proc.CloseMainWindow() | |
} | |
} | |
} | Out-Null; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment