Jump to content

Reading from the screen and send it to log file


AvN
 Share

Recommended Posts

Hi,

Quick question:I am trying to run an application using cmd.exe. How can I get the content of the commands that I have executed, from the screen and send it to a file?

Here is what I have try,Just an example, but it seems not to work

#include <file.au3>

$tempFile = ("C:\Documents and Settings\Avul\My Documents\temp\tempfile.txt"); creating a temp file

Local $console = Run ("C:\WINNT\system32\CMD, $STDERR_CHILD + $STDOUT_CHILD")

Run ("C:\WINNT\system32\CMD") ;opening cmd

WinWait ("C:\WINNT\system32\CMD.exe")

sleep(1000)

MsgBox(0, "","Console opened",2)

Send ("Help{enter}") ; sending a command

;in the following lines I am trying to take what ("Help{enter}") had put on the screen and send it to the tempfile

Local $line

While 1

$line = StdoutRead($console)

If @error Then ExitLoop

MsgBox(0, "STDOUT read:", $line)

Wend

$line = StdoutRead ("C:\WINNT\system32\CMD.exe")

;FileWrite($tempFile, $line)

_FileWriteLog ($tempFile, $line)

Your help will be much appreciated.

AvN

Link to comment
Share on other sites

Read the help file under Run() and StdOutRead(). You didn't set the $STDOUT_CHILD parameter on your Run() correctly. If you had, then StdOutRead() would get the data, but you won't see it on the screen anymore.

:)

Edit: Add working demo:

#include <Constants.au3>

Global $console = Run(@ComSpec & " /c HELP", @TempDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
Global $sMsg

Do
    Sleep(100)
    $sMsg &= StdoutRead($console)
Until @error
ConsoleWrite("$sMsg = " & $sMsg & @LF)
Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thank you for replying, I tried but looks like I am not getting it right. It doesn't work.

I understand that with run.... the command HELP is beign sent. Now is @TempDir the directory where it is being written?

How do I sent it to temperary file I create by my myself.

How do I sent it to C:\Documents and Settings\Av\My Documents\temp\tempfile.txt? so that I can read it there?

Thanks.

Link to comment
Share on other sites

I am going to copy PsaltyDS's code and change it a bit.

#include <Constants.au3>

Local $console = Run(@ComSpec & " /c HELP", @TempDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
; look up @TempDir. It is just the working directory - not really required in this case. PsaltyDS is smart and knows C:\Documents and Settings\Avul\My Documents\temp and @TempDir are the same 4 u.

;~ Global $tempFile = ("C:\Documents and Settings\Avul\My Documents\temp\tempfile.txt"); this does not create a file
Local $file = FileOpen("C:\tempfile.txt", 1) ; this creates the file. change the file path - @TempDir\tempfile.txt should even work.

Local $line
While 1
    ProcessWaitClose($console)
    $line = StdoutRead($console)
    If @error Then ExitLoop
    FileWrite($file, $line)
WEnd
FileClose($file)

Run("notepad C:\tempfile.txt")

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Thank you, this really helps. But how to open 2 commands at the same time so that I can get their contents in the file at the same time.

For example for HELP this is what is done.

Local $console = Run(@ComSpec & " /c HELP", @TempDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)

How can I add HELP AT command so that when HELP finish opening, HELP AT will also open in the same console and its sent to the same file as the content of HELP command?

Thanks.

Link to comment
Share on other sites

Not sure what you mean there. You can have two command windows open but they will be separate as they will be separate processes. You can append the output to "help at" the file by adding doing the same thing as you did for "help".

If you want "help at" sent after at, then you need to keep the command window open.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...