I seem to be a bit stuck on this and it seems like it should be really simple. I have been googling and trying various things for 3 hrs now and just can't get it.
Just a small caveat: the below code is not really my own (well the poorly coded bits probably are). I have gathered information from about 30 different sources to get it working and I didn't keep a bibliography so if some of it looks like your work, then thank you and sorry for not being able to give you credit.
What this script does is monitor a folder for jpg files which are generated by a tethered camera, when a new file arrives an input box pops up and asks for a new file name, then renames the new file, creates a copy in a subdirectory and calls an external exe which resizes the copied file to 500x500px.
As mentioned, it works, but I know it is messy and I can't seem to figure out how to validate that the user inputed information into the inputbox AND selected OK (as opposed to cancel, or no info inputed). I also can't seem to figure out how to declare some of those repeated addresses as $variables. The script failed with everything I tried.
Here's the script, any help you can provide will be great.
# DECLARE GLOBAL VARIABLES
$global:directory = "C:\Users\JoeBlogs\Desktop\Folder"
$filter = "*.jpg"
# SET FOLDER TO WATCH + FILES TO WATCH + NO SUBFOLDERS
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $global:directory
$watcher.Filter = $filter
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
# DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $latest = dir $global:directory | Sort CreationTime -Descending | Select name -First 1
$name = $latest.name
$basename = $name.ToUpper().replace(".JPG","")
Add-Type -AssemblyName Microsoft.VisualBasic
$newname = [Microsoft.VisualBasic.Interaction]::InputBox('New file name', 'Rename File', $basename)
Rename-Item $global:directory\$name $newname".jpg"
if (!(Test-Path -path $global:directory\"Resized")) {New-Item $global:directory\"Resized" -Type Directory}
Copy-Item -path $global:directory\$newname".jpg" -destination $global:directory\"Resized"\$newname".jpg"
&"C:\Users\JoeBlogs\Desktop\PhotoResize500x500IOQ100.exe" $global:directory"\Resized\"$newname".jpg"
}
# DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 3}