Jump to content
Sign in to follow this  
tr1px

Auto FTP Downloader using cmd.exe

Recommended Posts

tr1px

Corrected a problem in the function readOut() !!! Now it works like it is suppose to.

This script opens up a command prompt and establishes an FTP connection to a server then downloads 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 - return 0 if it does
    If $match = 0 Then                                ;i had to correct this part
        $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

Share this post


Link to post
Share on other sites
tr1px

I had to make a correction to this script

in the function readOut() $match = StringInStr($line, "ftp>") actually is wrong and has been changed to StringCompare($line, "ftp>") which returns a 0 if it matches.

Sorry for the mistake.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.