Automated FTP Script

Author: Carlos Lorenz
Return to REBOL Cookbook

Everyday at office I have to upload 3 different files from 3 different directories on the local server to 3 different destinations on an FTP server and after that I have to notify by email the persons that are responsible for each one of the files. Of course there must be more elegant ways of having this job scrippted but here is a simple one:

    #!/home/knoppix/rebol/rebol -qs 

    REBOL [ 
        Title: "Auto FTP" 
        File: %myftp.r 
        Date: 02-Sep-2003 
        Purpose: {Uploads 3 files from local server to an FTP site} 

    ; internet setup
    set-net [ 

    ; URL of FTP server
    site: ftp://user:pass@meuservidor.com.br/ 

    ; files on local server
    arquivos-servidor2: [ 

    ; destination directories on FTP server
    diretorios-ftp: [ 

    ; emails to be notified
    email-responsaveis: [ 

    print newline 

    ; traverses the local server files block 
    foreach file arquivos-servidor2 [ 

        ; picks first element of destination 
        ; directories and emails blocks
        dest: first diretorios-ftp 
        resp: first email-responsaveis 

        ; mounts nomearquivo 
        set [diretorio nomearquivo] split-path file 
        nomearq: rejoin [site dest "/" nomearquivo] 

        ; deletes old files at FTP server
        if exists? nomearq[ 
            delete nomearq 

        ; transfers the files
        write/binary nomearq read/binary file 

        ; prints a warning message to the screen
        print [file "sucessfully uploaded!"]

        ; assembles the body of the standard message 
        message: rejoin [ 
            "Hello user," 
            "This is an automatic message." 
            "The file ["nomearquivo"] was updated and is ready for download." 
            "Have a nice day!" 
            "Your REBOL Script" 

        ; sends emails
        send/subject resp message "Download notification" 

        ; get next element at destination directories and emails blocks
        diretorios-ftp: next diretorios-ftp 
        email-responsaveis: next email-responsaveis 

    print newline 
    print "File transfer finished" 
    wait 3

About the First Line

The first line of this script (the shell path) is optional. It is provided here for Linux, BSD, and other Unix systems to execute the script directly from the shell or from an automated time-based launcher such as Cron.

If you need this feature, change the first line to be the correct path to your REBOL program. Otherwise, you can remove the line.

2006 REBOL Technologies REBOL.com REBOL.net