Jump to content

Help with reading text from Command Prompt window


Recommended Posts

Hi everyone,

I'm a new AutoIt scripter. This is my first real attempt at making a script to make my life easier, and I've hit a problem with it: basically, I have no idea why it isn't actually working. This is the script:

CODE
Run ( "c:\Download\Programs\autoit\test.bat" ) ; Start an XXCOPY related batch file

WinWait ( "C:\WINDOWS\system32\cmd.exe", "", 90 ) ; Wait until the command prompt window is displayed, 90 seconds maximum

$text = WinGetText ( "C:\WINDOWS\system32\cmd.exe" ) ; Get any text from this window, helpfile says there is a limit of 64KB, assign to $text variable

If $text = "If your usage of XXCOPY does not qualify" Then

Sleep ( 2000 ) ; Sleep for 2000 milliseconds (2 seconds) to give script and window time to catch up to each other

Send ( "{ENTER}" ) ; If text is displayed, send Enter key to window

Exit

EndIf

Now, in that test.bat file, there is only the following (basically, this was the only way I could think of to test to see if the script actually worked before I used it in production):

CODE
@echo off

echo If your usage of XXCOPY does not qualify

pause

exit

Ok, so when I run the script, the Command Prompt window opens, the text 'If your usage of XXCOPY does not qualify' is displayed (without the '), and then on the next line you see the 'press any key to continue' prompt. Now, if my script was actually working, I wouldn't get to see that window because AutoIt would send an Enter to the window, thus closing it down. I've checked that script code over and over and over again - and I just can't see what the problem is. It's obviously not matching the text in the script against what is displayed in the window itself, but that is as far as I can take it (the text is identical - I copied it from the Command Prompt window to the script itself so I wouldn't make any typing/case related mistakes). I thought it may have been the 90 second wait in the WinGetText line, so I removed that and tried again - it made no difference. I thought it also might have been the order I had the Exit and EndIf statements - so I swapped them around (ie put the Exit under the EndIf), but it made no difference. I thought it might not have been sending the Enter correctly, so I changed that to {A} - no difference. I also removed that sleep line just in case it was causing a problem - that wasn't it either. Lastly, I tried a Dim $text line right at the very top (after reading the helpfile bit about variables), but that made no difference either. Nothing I've tried has made any difference in the way the script runs or the output of it. Would anyone be able to help me here?

Thanks,

Chris

Link to comment
Share on other sites

A command prompt does not have GUI controls and getting text from it the way you are doing won't work as far as i know. Use an STDOut stream to capture the output of the child.

Here, this is what I did:

$PID = Run ( "test.bat", "", @SW_SHOW, 2+4 ) ; The last parameter specifies that autoit will read the stderror and stdout of the child process.
WinWait ( @SystemDir & "\cmd.exe", "", 90 ) ; Wait until the command prompt window is displayed, 90 seconds maximum
While ProcessExists($PID)
    $s_STDOUT = StdoutRead ( $PID )
    
    ConsoleWrite(@LF & $s_STDOUT)
    
    If StringInStr($s_STDOUT,"If your usage of XXCOPY does not qualify",1) Then
        Sleep(2000)
        ProcessClose($PID)
        Exit
    EndIf
Wend

Very basic, you will probably want to make some changes, but it worked for me.

Also, the command prompt will not show text as the stdout stream is being sent to the parent process and not the command prompt, in this case, the autoit parent process.

Run this script in SCITE and use the console at the bottom to see the text that would normally be seen in the command prompt.

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

Another example is in the 'Func ExecuteCmd()'

in the following post

http://www.autoitscript.com/forum/index.ph...c=36245&hl=

execute the au3 file

enter your command in the input box at the top

hit go

stdout should appear in the large textbox at the bottom

Wallpaper Rotatorwith overlay, Loop through a folder of wallpaper & another of overlay, then create a combined image and set it as the wallpaperE-Mail passthru, Send any file, even executables via e-mail as plain text. The recipient can then later re-construct them.Slideshow widget, A slideshow widget similar to the Vista onePredictive typing using the Numpad, Predictive typing using the numpad of a keyboar similar to that on a mobile phone (the key is the .t16 file).PSTools Front End, For Remote Admin. Just makes life a lot easier (Demonstrates executing external programs and passing parameters, tabbed form Handling STDIN/STDERR)FTP Helper application Up and Download files from an FTP server demonstrates this and Tooltray TipsShow a Map of your Post-codes/Zip Codes, Uses the Clipboard, Hotkeys, the system tray (incl. menus)Disc/CD/DVD Catalogue, Ideal for all those Covermount Discs (Demonstrates Array handling, executing DOS programs, handling STDIN/STDOUT recursive directory reads, file searching.)YAST , Yet another Stopwatch/Timer (Uses a hotkey, Copies to clipboard, handles multiple events and stays on top)Keyboard Status Indicator , Indicates status of NumLock, Caps Lock and Scroll Lock Keys, demonstrates API calling & System tray Icon Toggling
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...