Jump to content
Sign in to follow this  
sqa

Command Prompt outputs

Recommended Posts

sqa

Hi, I am writing a script that writes commands to Command Prompt and have to set Sleep() to something approximate to let it to be executed for sure. Is there a possibility to wait until the certain line was printed out in Command Prompt, like when my command was executed Command Prompt returns to (let say) C:>?

Thanks

Share this post


Link to post
Share on other sites
water

Can you please post some example code?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
sqa

for example:

Run("cmd.exe")

1. WinWaitActive("Administrator: C:\Windows\System32\cmd.exe", "", 15)
2. Send('cd  C:/' &  "{ENTER}") 
3. Sleep(300)

4. send('mvn clean install > ' & $logFile  & "{ENTER}")
5. Sleep(10000)

so, sleep(10000) is a kind of long time to wait but I cannot reduce it in case if something happens and will take longer.

That's why I am wondering if there is a possibility to wait for a specific line in Com. Prompt to be sure that the last command (let say #4) was executed.

Share this post


Link to post
Share on other sites
tester123

1. WinWaitActive("Administrator: C:\Windows\System32\cmd.exe", "", 15)
2. Send('cd  C:/' &  "{ENTER}") 
3. Sleep(300)

4. send('mvn clean install > ' & $logFile  & "{ENTER}")
5. Sleep(100)

6.Do

7.Sleep (100)

8.Until

9.<<Add something unique to your process>> Could be ControlCommand + isVisible/isEnabled

Share this post


Link to post
Share on other sites
water

You can concatenate DOS commands using "&&" in a single string and then pass this string to Run:

$CMD = "command1 && command2"
Run(@ComSpec & " /c " & $CMD, "", @SW_HIDE)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
sqa

OK, but is there a possibility to wait for a certain line in DOS like let say:

WinWait("Administrator: Command Prompt", "C:Users")?

Share this post


Link to post
Share on other sites
water

Why do you want to wait or such a line? All commands passed to Run are processed in sequence.

If you replace Run with RunWait AutoIt gets control again after all commands have been processed.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Glad to be of service :D


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Chimp

OK, but is there a possibility to wait for a certain line in DOS like let say:

WinWait("Administrator: Command Prompt", "C:Users")?

Hi sqa

If you want to wait for a certain line in DOS output, You could use DOS I/O redirection to execute dos commands and read directly the output stream so you could check if the output contains the string you are waiting for.

The check string could be a "well known string" or you could concatenate an echo command with your "termination" string.

In this way, while the dos command is running, your script can do other activities, and you only sometime can check the output of dos command to see if your string is in the output

For example:

#include <Constants.au3>
Local $Output

; here we start a hidden DOS prompt to be used when needed (it remains alive becouse of /K parameter)
Global $prompt = Run(@ComSpec & " /K", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD + $STDERR_MERGED)
Do ; wait untill prompt is ready
    Sleep(100)
Until StringInStr(StdoutRead($prompt), ">")

; . . . do what you want with your script . . . .

; when you want run one or more dos command, simply use StdinWrite in this way:
; example of more commands in one line (set drive c: ; set root directory ; echo hello
StdinWrite($prompt, "c:" & @CRLF & "cd  C:\" & @CRLF & "echo hello" & @CRLF)

Do
    ; . . . do other things with your script . . . .

    ; sometime check the output by reading the output stream of previously sent DOS commands.
    ; here we check if the "hello" character has been "displayed"
    If StringInStr(StdoutRead($prompt), "hello") Then
        MsgBox(0, "", 'DETECTED "hello" in dos output', 5)
        ExitLoop
    EndIf
Until 1 > 1

; now a little longer time consuming command
; execute ping command and then execute an echo command
StdinWrite($prompt, "ping 127.0.0.1 -n 20" & @CRLF & "echo IHaveFinished" & @CRLF)
$Timer = TimerInit()
Do
    If TimerDiff($Timer) > 2000 Then ; every 2 seconds shows time just to show another activity
        ConsoleWrite("now It's " & @HOUR & ":" & @MIN & ":" & @SEC & " and dos command is still running..." & "(end string not detected)" & @CRLF)
        $Timer = TimerInit()
    EndIf
    $Output = StdoutRead($prompt)
    If StringInStr($Output, "IHaveFinished") Then
        ConsoleWrite("Finished" & @CRLF)
        MsgBox(0, "", 'DETECTED "IHaveFinished" in dos output', 5)
        ExitLoop
    EndIf
Until 1 > 1
ProcessClose($prompt)

bye

Edited by PincoPanco
  • Like 1

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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  

×