EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD

2 posts in this topic

#1 ·  Posted (edited)

Few times there was disscusion how to show/store ConsoleWrite() message from GUI based script.

This is my solution.

To check how this work you should:

  1. Download and compile to exe file this scirpt: TESTING.au3
    ConsoleWrite('Some text' & @CRLF)
  2. Download and compile to exe file this scirpt: ConsoleWrapper.au3
    #pragma compile(Console, true)
    #include <AutoItConstants.au3>
    #include <MsgBoxConstants.au3>
    #include <FileConstants.au3>
    If $CmdLine[0] = 0 Then Exit
    Func Example()
        Local $sCommand = $CmdLine[1]
        If $CmdLine[0] > 1 Then
            For $iCMD_idx = 2 To $CmdLine[0]
                $sCommand &= ' ' & $CmdLine[$iCMD_idx]
        Local $iPID = Run(@ComSpec & " /c " & $sCommand, @SystemDir, Default, $RUN_CREATE_NEW_CONSOLE + $STDERR_CHILD + $STDOUT_CHILD)
        Local $sOutput = _
                @CRLF & @CRLF & _
                'THIS IS LOG FILE FOR:' & @CRLF & _
                $sCommand & @CRLF & _
                'LOG START TIME: ' & _LogGetDate() & @CRLF & _
                'RESULT:' & @CRLF & _
                '=====================================' & @CRLF & _
        Local $hFile = FileOpen('ConsoleWrite_' & StringRegExpReplace(_LogGetDate(),'\D','_') & '.log', $FO_OVERWRITE + $FO_UTF8_NOBOM)
        FileWrite($hFile, $sOutput)
        While 1
            $sOutput = StdoutRead($iPID)
            If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            FileWrite($hFile, $sOutput)
        $sOutput = _
                '=====================================' & @CRLF & _
                'LOG END TIME: ' & _LogGetDate() & @CRLF & _
        FileWrite($hFile, $sOutput)
    EndFunc   ;==>Example
    Func _LogGetDate()
        Return @YEAR & '/' & @MON & '/' & @MDAY & '  ' & @HOUR & ':' & @MIN & ':' & @SEC
    EndFunc   ;==>_LogGetDate
  3. Run CMD
  4. in your CMD window you should make a command: ConsoleWrapper.exe TESTING.exe
    REMARK: as a parameter for: ConsoleWrapper.exe     you should use File Full Path for TESTING.exe file

I test it like this:

z:\Test>ConsoleWrapper.exe z:\Test\TESTING.exe

LOG START TIME: 2016/06/12  00:32:11
Some text
LOG END TIME: 2016/06/12  00:32:11


Enoy yourself for testing :)


Edited by mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post

Link to post
Share on other sites

I think this is a much easier and better way to capture ConsoleWrite output from a compiled GUI AutoIt Script :)

#include <Process.au3>

$sExe = @ScriptDir & '\GUI Script.exe'

$sLogFile = @ScriptDir & '\output.txt'

$sCommand = @ComSpec & ' /c "' & $sExe & '" > "' & $sLogFile & '"'



AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)


My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me:

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!



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