1

I have a simple bit of code below which runs an exe successfully 98% of the time. PowerShell waits for FineCmd.exe to close using | Out-Null.

Note: FineCmd.exe runs FineReader.exe

However, sometimes FineReader.exedisplays a simple error message box which stops the for loop. Once the user manually clicks OK and closes FineReader.exe, PowerShell continues with the for loop.

How can I get PowerShell to:

  1. realise that an error has occurred in FineReader.exe
  2. (force) close FineReader.exe
  3. repeat the loop iteration where the error occurred?

The entire code is below:

$path = "C:\OCR\process.txt"
$files = Get-Content $path
[System.Collections.ArrayList]$FPaths = $files
cd "C:\Program Files (x86)\ABBYY FineReader 12"

for ($i = $FPaths.count - 1; $i -ge 0; $i--) {
.\FineCmd.exe $FPaths[$i] /lang Mixed /out $FPaths[$i] /quit | Out-Null
$FPaths.RemoveAt($i)
$Date = Get-Date -Format g
Write-Host $i "Files to go at " $Date
$FPaths | Out-File $path
}
Ivan
  • 61
  • 7
  • What is the error? How is the error presented to you? – Clayton Lewis Sep 14 '18 at 19:59
  • 1
    Does the FineCmd.exe line usually take the same amount of time? Like, does it run and complete in a few seconds unless it errors? You could run it as a job, wait for the job to complete with a timeout. Then wrap it in a `Do/While` loop. Something like `Do{Get-Process FineRead -ea 0|Stop-Process -Force;Start-Job |Wait-Job -Timeout 30}While(Get-Process FineRead -ea 0)`. Otherwise you would need to somehow detect that FineRead.exe has an error, maybe run `Get-Process FineRead|% MainWindowTitle` to check for errors? – TheMadTechnician Sep 14 '18 at 20:22
  • The error is a message box [pop up](https://ibb.co/fu6UMe). The process takes varying amounts of time. I will try the `MainWindowTitle` approach. – Ivan Sep 16 '18 at 07:58

0 Answers0