Jump to content

Read text from cmd.exe ?


st3ady
 Share

Recommended Posts

Sorry if you consider this easy, but I am still new to AutoIt.

I have a script that downloads a zip file (containing an access database) and then using IZarc (a freeware unzipping program which is great!) I use the command line tool to unzip it. If the file is not newer, it will not unzip it, saying "-- no update needed" in my cmd.exe window.

If it does that, I want it to stop the script at that point. If it unzips successfully (because it is a newer version), I want to continue with my script which will open Access and run a macro in order to update my database.

I tried using the code:

$text = WinGetText("C:\WINNT\System32\cmd.exe", "no update needed")
MsgBox(0, "Text read was:", $text)

But it only returned a zero.

What function do I need to be using to determine if "no update needed"?

Many thanks ;)

Link to comment
Share on other sites

  • 2 weeks later...

Check out Run() and StdOutRead() .... :lmao:

Well I read as much as I could about about the StdOutRead in the help file and tried searching the forum for examples, it seems I keep seing the following in these threads, but I don't understand it or know how to implement it into my program correctly:

$iPID = Run(@Comspec & 'Whatever')
$sGather = ''
While Not @error
    $sGather &= stdOutRead($iPID)
WEnd
MsgBox(64, 'Info', $sGather)

Here is my program so far (I LOVE how it has automated so many tasks in just one click!):

#include <IE.au3>
; BEGIN DOWNLOAD OF ZIP FILE.

_IECreate("http://xxxxxxxxxxxxxxx/reporting/index.html")
Send("{TAB 28}")
Send("{ENTER}")
WinWait("File Download", "", 50)
Send("{ENTER}")
Send("{ENTER}")
Send("{TAB 1}")
Send("{ENTER}")
WinWait("Download complete", "", 50)
Send("{ENTER}")

; END DOWNLOAD OF ZIP FILE.

; BEGIN IZARCE EXTRACTION
Run("cmd.exe")
Sleep ( 250 )
Send("cd..{ENTER}")
Sleep ( 250 )
Send("cd..{ENTER}")
Sleep ( 250 )
Send("cd program files{ENTER}")
Sleep ( 250 )
Send("cd izarc{ENTER}")
Sleep ( 250 )
Send("izarce -sPRS -f -p")
$x = Chr(34)
Send("{" & $x & " 1}")
Send("E:\rrc8281\sos_program")
Send("{" & $x & " 1}")
Send(" ")
Send("{" & $x & " 1}")
Send("C:\WINNT\Profiles\Carsftp\Desktop\national_call_load_status.zip")
Send("{" & $x & " 1}")
Send("{ENTER}")
; END IZARCE EXTRACTION

I want to be able to do an if statement depending on the following screens:

If this happens:

Posted Image

then dont do anything, make a msgbox saying "No update needed", close the windows.

And if this happens:

Posted Image

then type y, press enter, and then go on to open microsoft access, run the macro, and then close everything.

Any help would be swell, thanks ;)

Link to comment
Share on other sites

Try this code.

$extractFolder = 'E:\rrc8281\sos_program'
$filenameToExtract = 'C:\WINNT\Profiles\Carsftp\Desktop\national_call_load_status.zip'
$command = '-sPRS -f -p"' & $extractFolder & '" "' & $filenameToExtract & '"'

$pid = Run('"' & @ProgramFilesDir & '\IZARC\izarce.exe" ' & $command, @ProgramFilesDir & '\IZARC', @SW_HIDE, 3)
Sleep(500)

If ProcessExists($pid) Then
    StdInWrite($pid, @CRLF)
    ConsoleWrite('+ sent "@CRLF"' & @CRLF)
EndIf

$data = ''
$count = 0

While ProcessExists($pid)
    $data = StdOutRead($pid, 1000, True)
    If @error Then
        ExitLoop
    ElseIf $data = '' Then
        ContinueLoop
    ElseIf StringReplace($data, 'Overwrite', '') Then
        If $count < @extended Then
            $count = @extended
            StdInWrite($pid, 'y' & @CRLF)
            ConsoleWrite('+ Sent "y & @CRLF"' & @CRLF)
        EndIf
    EndIf
    ConsoleWrite(@CRLF & '-- StdoutRead --' & @CRLF & $data & @CRLF)
WEnd

If $count Then
    ; Run MS Access
EndIf

I left the ConsoleWrite in in case of debugging is needed. I cannot admit I am pleased with the result as the full Stdout can not be attained with the prompt unless a @CRLF is sent 1st. VBScript Stdout had similar issues with it. It is hopefully good enough for your needs. If the prompt appears then $count should be not 0 so you can trigger the execution of MS Access by it.

;)

Link to comment
Share on other sites

wow thanks alot MHz, this is really cool! :evil:

It spit out around 500 copies of the following lines:

-- StdoutRead --

IZArc Command Line Extraction Add-On Version 1.0 (Build: 120)

Copyright© 2006 Ivan Zahariev, All Rights Reserved.

http://www.izarc.org contact@izarc.org

Archive File: national_call_load_status.zip

extracting: NFORM.MDB

Overwrite NFORM.MDB (y/n/a/q)?

And then stopped with this: >Exit code: 0 Time: 39.953

But I think it is working. I will try to read through your code to better understand what is happening ;) , and then attempt to do the MS access part. You da man ! :lmao:

Link to comment
Share on other sites

It spit out around 500 copies of the following lines:

Seems like you have a large file? As I mentioned, the ConsoleWrites are for debugging purposes so you can comment or remove them at any time. The last ConsoleWrite is being launched 500 times so you can comment it and you could add a Sleep(10) or more into the loop to allow some cpu for the extraction to speed up.

;)

Link to comment
Share on other sites

Thanks MHz.

Where should I put a message box that says "File was not new, thus there was no extraction or overwrite. Process has stopped."

I added the following at the bottom to run the macro in the access database file. I will definitely post this over in the Examples forum once it is complete ! ;)

If $count Then
   ; Open in MS ACCESS and run the macro called "run_reports"
    Run("cmd.exe")
    Sleep ( 250 )
    Send("cd..{ENTER}")
    Sleep ( 250 )
    Send("cd..{ENTER}")
    Sleep ( 250 )
    Send("cd program files{ENTER}")
    Sleep ( 250 )
    Send("cd microsoft office{ENTER}")
    Sleep ( 250 )
    Send("cd office{ENTER}")
    Sleep ( 250 )
    Send("msaccess.exe E:\rrc8281\sos_program\Region_SoS.mdb /x run_reports{ENTER}")
    Sleep ( 250 )
    Send("!{F4}")
    MsgBox(64, "MS ACCESS", "The macro run_reports was successfully ran.")
EndIf
Link to comment
Share on other sites

Where should I put a message box that says "File was not new, thus there was no extraction or overwrite. Process has stopped."

Try this version

If $count Then
    Run(@ComSpec & ' /c msaccess.exe E:\rrc8281\sos_program\Region_SoS.mdb /x run_reports', @ProgramFilesDir & '\microsoft office\office')
Else
    MsgBox(0x40000, Default, 'File was not new, thus there was no extraction or overwrite. Process has stopped')
EndIf

I am not sure of your Alt+F4, but you may want to use a WinWait and a WinClose to end? I'll leave to you to figure what is best.

;)

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...