Jump to content

Text from DOS program?


cronicl3
 Share

Recommended Posts

So my script runs an .exe (a DOS util), which runs (opening a DOS window) outputs some text in the window, then terminates (thus closing the window). How can I capture the outputted text into a .txt file from the DOS window before it closes? I know this is probably a very simple, stupid question, but I would really appreciate the help.

Thanks in advance.

Link to comment
Share on other sites

So my script runs an .exe (a DOS util), which runs (opening a DOS window) outputs some text in the window, then terminates (thus closing the window). How can I capture the outputted text into a .txt file from the DOS window before it closes? I know this is probably a very simple, stupid question, but I would really appreciate the help.

Thanks in advance.

Being it's your script, why not just change it so it writes to a file?

StdoutRead might be the other option

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

So my script runs an .exe (a DOS util), which runs (opening a DOS window) outputs some text in the window, then terminates (thus closing the window). How can I capture the outputted text into a .txt file from the DOS window before it closes? I know this is probably a very simple, stupid question, but I would really appreciate the help.

Thanks in advance.

It's not that simple, and it's not any stupider than I am (not saying much there)... :P

When you run the program with Run() you need to create a handle to the stdout from it (which makes it unavailable for other things, like being displayed on the console). You can then use that handle by way of StdOutRead(). Example with a console running "DIR /A:D /B" which gives you a simple list of subdirectories:

#include <constants.au3>
; Save process ID of Run()
$hRun = Run(@ComSpec & " /c DIR C:\*.* /A:D /B", @TempDir, @SW_SHOW, $STDOUT_CHILD)
; Read output
$sOutput = ""
While 1
     $sOutput &= StdOutRead($hRun)
     If @error Then ExitLoop
     Sleep(50)
WEnd
; Display results
MsgBox(64, "Results", $sOutput)

Cheers!

:nuke:

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

HI,

or nearly the same a bit easier.

#include <Constants.au3>
Global $text
Global $command = "ipconfig"

$stream = Run(@comspec & ' /c ' & $command,'',@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ControlSend("C:\WINNT\System32\cmd.exe", "", "", "n" & "{Enter}")
While 1
    $textline = StdoutRead($stream)
    $text = $text & @CRLF & $textline
    If @error Then ExitLoop   
Wend

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

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

It's not that simple, and it's not any stupider than I am (not saying much there)... :P

When you run the program with Run() you need to create a handle to the stdout from it (which makes it unavailable for other things, like being displayed on the console). You can then use that handle by way of StdOutRead(). Example with a console running "DIR /A:D /B" which gives you a simple list of subdirectories:

#include <constants.au3>
; Save process ID of Run()
$hRun = Run(@ComSpec & " /c DIR C:\*.* /A:D /B", @TempDir, @SW_SHOW, $STDOUT_CHILD)
; Read output
$sOutput = ""
While 1
     $sOutput &= StdOutRead($hRun)
     If @error Then ExitLoop
     Sleep(50)
WEnd
; Display results
MsgBox(64, "Results", $sOutput)

Cheers!

:)

Or you can just use:

RunWait(@ComSpec & ' /c DIR /ANYTHINGGOES > %TEMP%\output.tmp', "c:\", @SW_HIDE)

:nuke:

Link to comment
Share on other sites

Or you can just use:

RunWait(@ComSpec & ' /c DIR /ANYTHINGGOES > %TEMP%\output.tmp', "c:\", @SW_HIDE)

:nuke:

Of course you can. That point has been made repeatedly (see nobby post above). It is always sort of the lowest common denominator to run a DOS command and capture the output to a file. If you want to create and handle intermediate files, it works. The next level above that is capturing the output and using it in real time inside the script without an intermediate file. Remember some people do much more complicated things with that technique than a single DIR command illustrates. Another level above that, and you find ways to get what you need without creating a console shell at all, which takes a lot of time and cycles if you are repeating it in a loop, for example.

The OP asked about capturing output to a file, and your answer works great for that (as nobby's did). I just wanted to show that the intermediate file is not required. This is something I only recently figured out how to use and sharing things like that is part of what this forum is about.

Cheers again!

:)

P.S. As soon as I get to a Windows box, I want to check out ControlSend() to a console window, per th.meger above. More good stuff to learn and try! :P

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

thanks so much for everyone's help. I have some windows programs that I run with the following

Run(@ComSpec & ' /k ..\myproggy.exe /stext' , "output.txt", @SW_HIDE)

and that works, but it didn't for DOS windows.

going try out this when i get home:

RunWait(@ComSpec & " /c myDOSproggy.exe > a.tmp", @ScriptDir, @SW_HIDE)
Link to comment
Share on other sites

thanks so much for everyone's help. I have some windows programs that I run with the following

Run(@ComSpec & ' /k ..\myproggy.exe /stext' , "output.txt", @SW_HIDE)

and that works, but it didn't for DOS windows.

Note that the second parameter ("output.txt" above) is supposed to be the working directory to use, not a file name. See helpfile on Run().

going try out this when i get home:

RunWait(@ComSpec & " /c myDOSproggy.exe > a.tmp", @ScriptDir, @SW_HIDE)
The above code should work, and for both of these it may not be necesary to open a new shell with @ComSpec & " /c ". A command shell is only explicity required if you want to use one of the built-in shell commands like DIR. You should need it for anything that ends in .exe. These might also work:

Run('C:\MyDir\myproggy.exe /stext "output.txt"', @ScriptDir, @SW_HIDE)

; Or 

RunWait("C:\MyDir\myDOSproggy.exe > a.tmp", @ScriptDir, @SW_HIDE)

:)

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

Okay, it all seems to be working, however cmd.exe never closes. The script finishes but cmd.exe is still in the process tree. Here's the code:

Run(@ComSpec & " /k ..\utils\logapp.exe > ..\output\dump.txt", @ScriptDir, @SW_HIDE)
Sleep(200)
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]

[ [/C | /K] string]

/C Carries out the command specified by string and then terminates

/K Carries out the command specified by string but remains <---- Maybe the reason

Link to comment
Share on other sites

Thanx guys the script from Mega is working fine for me :)

#include <Constants.au3>
Global $text
Global $command = "net view"

$stream = Run(@comspec & ' /c ' & $command,'',@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ControlSend("C:\WINNT\System32\cmd.exe", "", "", "n" & "{Enter}")
While 1
    $textline = StdoutRead($stream)
    $text = $text & @CRLF & $textline
    If @error Then ExitLoop   
Wend

;~ MsgBox(0, "STDOUT read:", $text)
FileWrite ("C:\Test.txt",$text)


$file = FileOpen("C:\test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If $line = StringInStr("\\ADMINISTRATIE-2","") then 
        msgbox (0,"","found!")
        ExitLoop
    EndIf
    
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)
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...