Sign in to follow this  
Followers 0
zauberberg

read from console, and sending parameters

8 posts in this topic

Hi all,

I have a console program that, run basically as text iteration and you have to pass parameters as the program ask you to enter it. I would like to do some automation scripts, capture the output (text in the console) and write it on a file. then analyze the results on the file.

I've tried with the StdoutRead function but is quite inestable, some times all output is redirected, another time I don't know why i can not read the parameters the program asks...

My question is, is AutoIt a good tool to do that job? I'll need redirect standard output, but maintain standard input, so I can use send function to the program and capture the console output. That is, in some way confuse the program to think is sending the parameters to the console but is sending them to a file (both would be great but by now its enough only redirecting)

Thanks in advance,

Juan

Here my solution:

Local $pid=Run('consoleprog.exe parameter',"consoleprogdir",  @SW_MINIMIZE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
Local $data
$line=""
WinWaitActive("conlsoeprogwindow")
 
While 1
    If WinExists("conlsoeprogwindow") Then
        WinActivate("conlsoeprogwindow")
        while 1
        $line &= StdoutRead($pid)
        InputBox("test stdoutread", $line)
            If $line <> "" Then
                FileWrite("call_logs\proglog",$line)
                InputBox("test stdoutread", $line)
                WinWaitClose("test stdoutread")
                $line=""
            ExitLoop
      
        EndIf
        WEnd
    EndIf
    WinWaitClose("Introduce Parametro")
    $input= InputBox("Introduce Parametro", "Parameter")
  
    sleep(800)
  
    If WinExists("C:\Users\jperezes\Desktop\latestecc\ECC_Jabber\out\bin\TestApp.exe") Then
        WinActivate("C:\Users\jperezes\Desktop\latestecc\ECC_Jabber\out\bin\TestApp.exe")
    ConsoleWrite($input & @CRLF) ; anther program catch the window and write the parameters the console program asks.
    Else
        ExitLoop
    EndIf
Wend

Share this post


Link to post
Share on other sites



Hi,

Thanks for quick response, but if I do that it's even worst, I never get out from the do while loop.

In my original script i am already in a loop but without catching the error, its completely necessary to catch the error? and another question, I'd like to know how stdoutread works, that is, read line by line, read all that appear in standard output, cause in theory my program show lines in the windows console and you have to introduce parameters, that is, IP addres, user and password. then after connect interact with you through the console too, that is you type keys and text appear in the console.

By now, i don't get to redirect my console to a file...

Thanks,

Juan

Share this post


Link to post
Share on other sites

Iirc you need to call it in a loop until an error occurs, something like this:

Do
      $Line &= StdoutRead($pid)
      Sleep(20)
Until @Error
FileWrite("call_logs\proglog",$line)

That won't work because the Sleep(20) line resets the @error, you would need to put the sleep before the $Line &=...

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

Iirc you need to call it in a loop until an error occurs, something like this:

Do
      $Line &= StdoutRead($pid)
      Sleep(20)
Until @Error
FileWrite("call_logs\proglog",$line)

This method only got an incomplete set of results from some CMDs needing more time and more return chars, like :Systeminfo,

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

This method only got an incomplete set of results from some CMDs needing more time and more return chars, like :Systeminfo,

zomg

Do
      $Line &= StdoutRead($pid)
      Sleep(20)
Until NOT ProcessExists($Pid)
FileWrite("call_logs\proglog",$line)
Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If you're gonna wait for the app to exit you could ProcessWaitClose() and read the data afterwards.

(not sure if there's a limit to the STDOUT stream so I would test it thoroughly)

Pseudocode=

pid = Run(app)

ProcessWaitClose(pid)

result = StdoutRead(pid)

Saves a couple lines of code.

(I do this in Who's up?)

Edited by AdmiralAlkex

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