1

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}

1 Answer 1

1

Validate that the user inputed information into the inputbox AND selected OK

If user presses cancel or leaves input field empty, then $newname is empty. This can be used in an if statement to make sure the renaming only takes place if input is OK:

$newname = [Microsoft.VisualBasic.Interaction]::InputBox('New file name', 'Rename File', 'ddd')
if ($newname) {
  Rename-Item $global:directory\$name $newname".jpg"
...
}

Additional Improvents

Declare additional variable resized_directory as it used several times later:

$global:resized_directory = "C:\Users\JoeBlogs\Desktop\Folder\Resized"

This line should be moved to the top, because this needs to be done only once:

if (!(Test-Path -path $resized_directory)) {New-Item $resized_directory -Type Directory}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much Aedvald, I implemented all your suggestions and everything works perfectly. One minor annoyance is that the InputBox doesn't have focus and I have to manually click into it in order to edit. I might try Ryant's solution: stackoverflow.com/questions/9978727/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.