Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

IF and WHILE are language elements that evaluate conditions. You may use them for evaluating error conditions, performing actions a fixed number of times etc. Since every variable in ScriptFTP contains text, the text “TRUE” means true. Any other text is evaluated to false. For example:

IF("TRUE")
    PRINT("ScriptFTP rules")
END IF
 
IF("fdasfdas")
    PRINT("hi")
END IF
 
$myvariable="TRUE"
 
if($myvariable)
    PRINT("ScriptFTP rules")
END IF

And the output is:

ScriptFTP rules
ScriptFTP rules

As you can see from the script output the IF statement only executes its body if the condition equals the text “TRUE”.
Let us look at a more useful example. In the following script the IF statement is used to check whether OPENHOST was successful:

# Connect to ftp.myhost.com and download
# all files in the www folder.
#
#
# The server and login are:
$host="ftp.myhost.com"
$user="carl"
$password="1234567890"
 
# This is a label, it marks a point
# in the script file
:start
# Connect to FTP server
$result=openhost($host,$user,$password)
 
# check what happened with openhost
IF($result=="OK")
    PRINT("CONNECTED")
ELSE
    PRINT("Cannot connect. Waiting 5 seconds.")
    SLEEP(5)
    GOTO :start
END IF
 
# do the stuff
LOCALCHDIR("C:\localwww\")
CHDIR("www")
GETFILE("*.*")
 
CLOSEHOST()
PRINT("FINISHED")
EXIT

The WHILE statement will execute the commands in its body as long as the condition equals “TRUE”. In the following script the WHILE statement will never terminate and will therefore keep a local directory synchronized with an FTP site:

# This script will loop indefinitely and always keep a
# local directory synchronized with an FTP site.
#
#
# Server and login information
$host="172.16.0.4"
$user="carl"
$password="123456"
 
# Connect to server
OPENHOST($host,$user,$password)
 
# Change current remote directory
CHDIR("remotedir/remotesubdir")
 
# Change current local directory
LOCALCHDIR("C:\localdir\")
 
# Loop indefinitely
WHILE("TRUE")
    $result=GETFILE("*.*")
    IF($result!="OK")
        PRINT("ERROR in GetFile, exiting")
        # Exit with error code 1
        EXIT(1)
    ELSE
        PRINT("All files downloaded. Waiting 10 seconds")
        SLEEP(10)
    END IF
END WHILE

Next Lesson: Handling file lists