Sign in to follow this  
Followers 0
jeris

Can the Colsole output be saved to a file (as formatted)?

10 posts in this topic

When ConsoleWrite is used in an autoit script, can that output be saved to a log file with formatting as it appears in the console window? Or, can a script be set up to log the information to a file instead of the console? What happens when the script is made an executable (so far I've just been using the "Tools>Go" to run a script)?

Thanks in advance for any help.

- Newbie

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Whether there is a console or not does not actually matter. There is always an output stream.

The easiest way to make this write to a file is by using the '>' symbol when running it. Try "SomeProgram.exe > SomeFile.txt" and you'll see it outputs the file with what would have been in the console.

In AutoIt you can read from the ouput stream of other programs (or the current one) using StdoutRead, so that is the AutoIt solution. But it is probably easier using redirection as mentioned above.

Edit: Just noticed the "as formatted" bit. You are going to have to define that a bit better. A few things that I would class as that is the width (so limit the number of characters per line, but that depends on the console. It's usually 80 but you can't rely on that. You could find it with _Console_GetScreenBufferSize or using the api calls I based that on, but there won't always be a console when you are streaming to a file). Then there is text colour and background. The console uses a very different setup for colours so it will take some programming... Then there is the file format itself. rtf? html? You'll need a format capable of describing colours...

Edited by Mat

Share this post


Link to post
Share on other sites

Some output is written when I compile it as CUI and run it with the ">" pipe, but it hangs. Runs fine in the editor (the script opens a telnet session, is this not possible when it's changed to an executable?)

Thanks.

Share this post


Link to post
Share on other sites

Try Mat's solution.

That was my solution :)

Some output is written when I compile it as CUI and run it with the ">" pipe, but it hangs. Runs fine in the editor (the script opens a telnet session, is this not possible when it's changed to an executable?)

Thanks.

Can you provide something simple we can test? I understand if this is not possible in this case.

Usually something like that will hang when it is expecting the user to type something into the console. What exactly is it trying to do when it hangs?

Share this post


Link to post
Share on other sites

The script I've created is performing a series of power cycle on a remote computer platform.

> It opens a command prompt window, starts Telnet, creates a Telnet log file and the opens a session with the remote PC and tells it to shut down and then closes the telnet session with the remote PC.

> It then opens a new telnet session with a power cycling device (iBootBar) and tells it to power down the outlet into which the remote PC is connected and then power back up. It then closes the Telnet session with the iBootBar.

> After sleeping for awhile to allow the PC to come back up, it executes a Bat file which simulates some transactions with the remote PC and verifies the results.

This is repeated for as many power cycles as defined (the number of power cycles is a constant in the script - editable for different numbers of iterations). I've had it run 100 power cycles over night from the editor (using Tools>Go) and it appears to work without any user input required...

I'm not much of a programmer, so it might look like spaghetti to you guys, but I've attached a copy of the script. I'm supposed to run a thousand cycles over the weekend...

TextSearchUtils.au3

AC_Power_Cycle.au3

Share this post


Link to post
Share on other sites

Firstly, you are using a very un-elegant way (not quite spaghetti, just using a very basic approach) to enter the commands. I imagine this is probably where the problem lies. Try to avoid using Run("cmd") and then sending input. There are better ways of going about that. Similarly, this means you have to use Sleep(1000), which I imagine is not what you really want. I can't directly answer your question with what you have there, as I can't really test it. However, I can offer you the following changes which (though might not fix the problem just yet) improve the code a lot.

Try to make the code work without a single Send statement, they cause all sorts of problems and are actually rather limited. To do this you will need to use StdinWrite (see the example in the helpfile for an example of how to use PIDs and standard streams. This will also mean you won't need to use Run("cmd"), you can run telnet directly and write to it's standard input. I'd actually run it in a separate console, and read the output using StdoutRead, which you can then write to the logfile directly (or echo them back out with ConsoleWrite). This will mean you will be able to detect when an operation is finished, allowing you to avoid using Sleep. It will also mean your program will run completely in the background, even while you are using the computer, possibly even without any terminals cluttering the screen too.

That may seem like a lot, but once you understand it it's not too complicated. Perhaps test it out first and see what works. You may want some utility functions, there is one on the forum somewhere (link escapes me) that runs a cmd command and reads all the output, returning it as a single string. That's something you'll have to do a lot if you use the method I described above. As already said, I imagine it is waiting for user input and not getting any. I would hope making other improvements to the code would solve it, or at least make the problem clearer.

Feel free to ask if you need any more help :)

Share this post


Link to post
Share on other sites

Thanks so much for your help. I'll give it a try!

Share this post


Link to post
Share on other sites

Well, I'm back - I broke the computer before I got to complete my test so, while they're trying to fix it, I've been trying to change my script per your recommendations - "un-elegant" was a very nice way to put it, by the way.

I looked at the StdinWrite help and thought I'd use that example to see if I could run a simple telnet session. I tried:

#include <Constants.au3>

Local $foo = Run ("telnet.exe", @SystemDir, $STDIN_CHILD + &STDOUT_CHILD) ; didn't hide window (@SW_HIDE)

StdinWrite($foo, "set logfile test.log" & @CRLF)

StdinWrite($foo)

I thought that this would at least just open a telnet log file (test.log) just to see if the StdinWrite was working, but the telnet window just opens and waits for user input. Can you tell me what I'm doing wrong? I apologize for being dense - I am not a programmer...

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