Jump to content

Recommended Posts

Posted

Hi all,

I would like to run a program in a dos box (using cmd.exe). This works well. After the program started it ask me to type random characters. After a while the program tells me that it is enough. What i would like to do is to automate this random character typing.

The steps of the program:

start program

type random characters by user

program reports that's enough.

Is there a way to notice that the program is finished?

I hope my question is clear, if not please let me know.

Thanks in advance.

Kind regards,

Maarten

Kind regards,Maarten

Posted

Try to use Send or ControlSend to type a random characters

From the help file:

ControlSend is only unreliable for command prompts.........

[size="1"][font="Arial"].[u].[/u][/font][/size]

Posted

I understand what you are asking because I have the same problem right now. Here is my code...

Run("cmd.exe")
WinWaitActive("C:\WINDOWS\system32\cmd.exe")
Send("cd c:\ftpdl")
Send("{ENTER}")
Send("ftp -i")
Send("{ENTER}")
Sleep(3000)
Send("open someftpserver.net")
Send("{ENTER}")
Sleep(12000)
Send("username")
Send("{ENTER}")
Sleep(4000)
Send("password")
Send("{ENTER}")
Sleep(10000)
Send("mget *.csv")
Send("{ENTER}")
$exitNum = 5
While $exitNum = 5
    Send("!{SPACE}")
    Send("e")
    Send("s")
    Send("!{SPACE}")
    Send("e")
    Send("y")
;here I need to get the clipboard
       ; save it to a file and read the file
       ; then I need to compare the last line and continue...

Where the While statements starts I am starting a loop that selects the text of the command prompt and copies it. Now I need to copy the clipboard to a file and have autoit read the last line of the file and use an if statement to exit the program if it matches >ftp which will indicate that the download completed but I am stuck and do not know how to go about it.

Thanx in advance!

Posted (edited)

All right I figured it out... After sitting on the couch for a few hours... By the way I just found out about Autoit v3 yesterday.

Here is the code... It opens up a command prompt and establishes an FTP connection to a server then download the whole directory of files. Now in order to run other commands after the process is finished I needed to be able to get the output from the prompt so I copy and paste the text into a file in a loop and search for the line that tells me that it is finished which in this case would be ftp>. Now that I know it is done I can run other commands like delete the files of the server they are sitting on.

Here is the code finally...

#include <File.au3>

Run("cmd.exe")
WinWaitActive("C:\WINDOWS\system32\cmd.exe")
Send("cd c:\ftpdl")
Send("{ENTER}")
Send("ftp -i")
Send("{ENTER}")
Sleep(3000)
Send("open ftpserver.net")
Send("{ENTER}")
Sleep(12000)
Send("username")
Send("{ENTER}")
Sleep(4000)
Send("password")
Send("{ENTER}")
Sleep(10000)
Send("mget *.csv");start the download process
Send("{ENTER}")
$exitNum = 5        ;any number that will keep the loop going
While $exitNum = 5
    Send("!{SPACE}") ;this segment copies the output
    Send("e")
    Send("s")
    Send("!{SPACE}")
    Send("e")
    Send("y")
    $outPut = ClipGet();here we get the output from the clipboard
    writeOut($outPut)  ;send the output to our function
    readOut()
WEnd

Send("Finished");here we can run more commands or close the prompt or do whatever

Func writeOut($data)
; Write the data to a file
    $file = FileOpen("commanddata.dat", 2);
    FileWrite($file, $data)
    FileClose($file)
EndFunc

Func readOut()
    $file2 = FileOpen("commanddata.dat", 0)
    $lastline = _FileCountLines("commanddata.dat")
    $line = FileReadLine($file2, $lastline)
    $match = StringCompare($line, "ftp>");check if the download is complete
    If $match = 0 Then
        $exitNum = 6
        Return $exitNum
    Else
        $exitNum = 5
        Return $exitNum
    EndIf
EndFunc

Hope you guys find this useful. This is my first little program that I wrote in AutoIt.

Edited by tr1px
Posted

Try this it usually works for me

Dim $slogfilepath="C:\mylog.log" ,$DriveName ="C:", $pada ='\' , $code ='0017'

$sServerString="%s /c cd %s%sdir%s"

$sChangeCmd=StringFormat($sServerString, @ComSpec,$DriveName,$pada,$code)

_FileWriteLog($slogfilepath, "commnand : " & $sChangeCmd)

$schangeresult =Run($sChangeCmd,"",@SW_HIDE, 2+4)

$sChangeOutVar = ""; buffer for StdOut output

$sChangeErrVar = ""; buffer for ErrOut output

;MsgBox(4096, "Result3 ", $sMapCmd)

While 1

$sChangeOutVar = StdoutRead($schangeresult)

$sChangeErrVar = StdErrRead($schangeresult)

If @error Then ExitLoop

Sleep(100)

WEnd

_FileWriteLog($slogfilepath, "Result of Directory change to " & $Drivename & " \dir" & $code)

_FileWriteLog($slogfilepath, "Result : " & $schangeOutVar)

_FileWriteLog($slogfilepath, " Error code : " & $schangeErrVar)

_FileWriteLog($slogfilepath, "Finished changing directories "& $DriveName & "\dir" & $code)

Posted (edited)

Here's what you want?

$iPID = Run(@ComSpec & " /c " & $command, @TempDir, @SW_HIDE, $STDOUT_CHILD)
While 1
    $sOutput &= StdoutRead($iPID)
    If @error Then ExitLoop
WEnd
msgbox(0,"",$sOutput)

After you get the text you want to hear, have it start sending keys.

I beleive there is an option, not sure if it works with cmd...

winkeepactive, I think it is?

After you get the command, instead of messagebox, do send.

Edited by BackStabbed

tolle indicium

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
×
×
  • Create New...