Sign in to follow this  
Followers 0
smkiat

Parse and process verbose output from non-AutoIT exe file in Command Prompt

9 posts in this topic

Hi,

Been at this since yesterday but have no scripting or VB skills. I am working with a non-AutoIT executable that provides verbose output when run in a Command Prompt window. When there is a failure the verbose out put includes a single instance of the string ABORT. I do not have the skills to figure out how to parse the string and use it to programmatically either move folder content and close the Command Prompt window or simply close the Command Prompt window if the ABORT string does NOT exist.

; Run Command Prompt
Run("cmd.exe")
; Wait for the Command Prompt to become active - it is titled "C:\WINDOWS\system32\cmd.exe" on this system and is case sensitive.
; If active run faxin.exe which is not an AutoIT script.
WinWaitActive('C:\WINDOWS\system32\cmd.exe')
If WinExists ('C:\WINDOWS\system32\cmd.exe') Then
WinActivate ('C:\WINDOWS\system32\cmd.exe')
; Run Castelle faxin.exe to process inbond faxes.
Send ('FaxIn.exe {ENTER}')
EndIf
; THIS IS WHERE I NEED HELP - If there is an error the faxin.exe verbose output includes the word "ABORT" without quotes.
; I am too ignorant to figure out how to programmatically parse for the ABORT string. Also help with the logic to to either
; run the following if an ABORT string exists or go directly to the Send('exit {Enter}') if the ABORT string does not exist.
; If ABORT error occurs move files to error Folder for review. This clears the FaxIn Folder for further inbound fax processing.
Send('move /Y E:\FaxStorage\FaxIn\*.* E:\FaxStorage\FaxIn\error {Enter}')
; Once the file move is complete or if no ABORT error is reported exit the Command Prompt.
Send('exit {ENTER}')
; Finished!

The script runs as expected but is useless without the logic. I have attached two text files. One is the verbose output when there is an error and one when there is no error.

Thanks in advance for any help with this.

Sean

FaxInError.txt

FaxInNoError.txt

Share this post


Link to post
Share on other sites



Smkiat,

Welcome to the forums.

You've realised that you need to get the output and parse it. Two ideas you can try:

1. Get output to file by putting " > output.log " on the end of the command. Standard DOS option. Then see AutoIt help on FileOpen and FileRead. Search text in memory with StringInstr.(..., "Abort")

2. On cmd window grab output with alt-space-edit-select_all, alt-space-edit-copy. Try by hand, then with Send commands. See AutoIt help on ClipGet.

Richard.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Try this:

; Demonstrates StdoutRead()
#include <Constants.au3>

Local $foo = Run('FaxIn.exe', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    If $line <> "" then ConsoleWrite( "STDOUT read:", $line  & @CRLF)
WEnd

While 1
    $line = StderrRead($foo)
    If @error Then ExitLoop
    If $line <> "" then ConsoleWrite( "STDErr read:", $line  & @CRLF)
WEnd

MsgBox(0, "Debug", "Exiting...")

This is the example script taken from the help file for StdOutRead with your program name being put in there.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

smkiat,

Try this adaptaqtion of BrewmanNH's post

; Demonstrates StdoutRead()
#include <Constants.au3>

Local $foo = Run(@ComSpec & " /c " & 'faxin.exe', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    If stringinstr($line,'recognized') > 0 then     ; change this to "Abort" or whatever you are looking for
        ;do some stuff
        consolewrite("found string in this line:  " & $line & @lf)
    endif
    if $line <> "" then consolewrite($line & @lf)   ;just to show that it works
WEnd

While 1
    $line = StderrRead($foo)
    If @error Then ExitLoop
    If stringinstr($line,'recognized') > 0 then     ; change this to "Abort" or whatever you are looking for
        ;do some stuff
        consolewrite("found string in this line: " & $line & @lf)
    endif
    if $line <> "" then consolewrite($line & @lf)   ;just to show that it works
WEnd

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Thanks for the responses. RichardL, I was able to use the information you provided to create output.log files but I must be the village idiot as I cannot get the following to give me a Message Box response of anything other than 0. I cannot seem to figure out the StringInStr function. But I figure I need to get a 1 in order to run some type of If...Then. Even if I populate a text file with a single character and search for that particular character the Message Box returns 0. Not sure where I am going wrong.

; $file is the FileOpen File Handle
Local $file = FileOpen("C:Tempoutput.log", 0)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Read in 1200 characters at a time until the EOF is reached
While 1
    Local $chars = FileRead($file, 1200)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Char read:", $chars)
; $var is the string "ABORT"
Local $var = String('ABORT')
Local $result = StringInstr($file, $var, 0, 1, 1, 1200)
MsgBox(0, "Search result:", $result)
WEnd
FileClose($file)

BrewManNH & kylomas, I tried using the example provided and can tell that a console window runs, I can output the contents to a text file and know it is working at the most basic level. That said I did not figure out how to view the ConsoleWrite output so I can see what is happening for testing purposes. Here is one of many variants of the test code I tried. In this version i did not hide the window so I can see if it opened and I dumped output to a text file to make sure something happened. In this case since I am not seeing the ConsoleWrite output I cannot tell what the StringInStr function is doing.

#include <Constants.au3>
Local $foo = Run(@ComSpec & " /c " & 'FaxIn.exe >E:FaxStorageFaxInParseoutput.log', "", $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
$line = StdoutRead($foo)
If @error Then ExitLoop
If StringInStr($line, 'ABORT') = 0 Then ; change this to "Abort" or whatever you are looking for
  MsgBox(0, "Search result:", $line)
  ;do some stuff
  ConsoleWrite("found string in this line:  " & $line & @LF)
EndIf
If $line <> "" Then ConsoleWrite($line & @LF) ;just to show that it works
WEnd
While 1
$line = StderrRead($foo)
If @error Then ExitLoop
If StringInStr($line, 'ABORT') = 0 Then ; change this to "Abort" or whatever you are looking for
  MsgBox(0, "Search result:", $line);do some stuff
  ;do some stuff
  ConsoleWrite("found string in this line: " & $line & @LF)
EndIf
If $line <> "" Then ConsoleWrite($line & @LF) ;just to show that it works
WEnd

Thanks again for looking at this.

Sean

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

smkiat,

Are you running this under the full version of SCIte?

kylomas

edit: correction - you are missing a parm in

Local $foo = Run(@ComSpec & " /c " & 'faxin.exe', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

If you want to see the command window you need to change the parm on the @comspec invocation. I don't recall what the syntax is, however.

Run the following under SCiTE

; Demonstrates StdoutRead()
#include <Constants.au3>

Local $foo = Run(@ComSpec & " /c " & 'faxin.exe', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    If stringinstr($line,'recognized') > 0 then     ; change this to "Abort" or whatever you are looking for
        ;do some stuff
        consolewrite("found string in this line:  " & $line & @lf)
    endif
    if $line <> "" then consolewrite($line & @lf)   ;just to show that it works
WEnd

While 1
    $line = StderrRead($foo)
    If @error Then ExitLoop
    If stringinstr($line,'recognized') > 0 then     ; change this to "Abort" or whatever you are looking for
        ;do some stuff
        consolewrite("found string in this line: " & $line & @lf)
    endif
    if $line <> "" then consolewrite($line & @lf)   ;just to show that it works
WEnd

If you want an output file then write one in the read loop.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

smkiat,

To see the output on the console try taking out the piping to the file. If you want to keep the piping then post-process the file, no need for trapping the sysout.

Sorry for the terse explanation but I can read faster than the connection that I am on...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

After a lot of google fu and many dead ends I now have a piece of code that appears to do what I want without any obvious issues. My concern is the FaxIn.exe executable that my AutoscriptIT FaxInUNW.exe execuatable calls is run every 5 minutes 24x7 in Scheduled Tasks. Would knowledgeable members be kind enough to tell me if they see any issues with what I fianlly came up with?

; Includes provide additional functionality
#include <Constants.au3>
#include <Array.au3>
#include <File.au3>
; Run FaxIn.exe in hidden command interpreter and pipe output to text file named output.log
Run(@ComSpec & " /c " & 'FaxIn.exe >E:FaxStorageFaxInParseoutput.log', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
; Set Variants
Local $sPath = "E:FaxStorageFaxInParseoutput.log"
Local $sTextToFind = "------------------------------------------ ABORT"
Global $aArray
; Get outputlog file into array
_FileReadToArray($sPath, $aArray)
; Start at the beginning
$iStart = 0
; Run loop to parse output.log file to check for ABORT string. If found move all files in FaxIn folder to FaxInError folder for later review.
; Moved files will overwrite existing if file names are the same.
While 1
  ; Look for the line including the string ABORT
  $iIndex = _ArraySearch($aArray, $sTextToFind, $iStart)
  ; If no matches for ABORT then exit loop
  If $iIndex = -1 Then
   ExitLoop
  Else
   FileMove("E:FaxStorageFaxIn*.*", "E:FaxStorageFaxInError", 1)
  EndIf
ExitLoop
WEnd
Edited by smkiat

Share this post


Link to post
Share on other sites

smkiat,

Without the faxin mod cannot test. If you elect to pipe to a file and post-process the output this should work, however, you can process the entire file as a string without spinning through an array.

Example

$infile = fileread("E:FaxStorageFaxInParseoutput.log")
if stringinstr($infile,$stexttofind) > 0 then
   ; do found string stuff
else
  ; do did'nt find string stuff
endif

None of this is tested but you get the idea.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0