Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

Every ScriptFTP command returns a text string to the script once the command has finished. This text string is not the message that is usually shown on the ScriptFTP window below each command, it is a short text value that the script can internally handle to check if the execution of the command was successful or not. If everything goes well it will return the text “OK”, if something goes wrong you will get an error code. Evaluating this return value and taking the appropiate measures you can make your file transfers fault-tolerant.

In the following example the output of OPENHOST is stored in a variable called $result. If $result is “OK” we will continue, if $result is different from “OK” we will show a message and try to reconnect to the FTP server.

# This is a label. It marks a point in the script.
# We will use it to return to this point if a
# connection attempt fails.
:connect
 
# Shows a message
PRINT("_____Connecting_____")
 
# Connect to server. The return value of OPENHOST
# is stored in $result
$result=OPENHOST("myserver.com","me","13579")
 
# Check if $result is different from "OK"
IF($result!="OK")
    PRINT("Cannot connect! Trying again.")
    # Jump to the label :connect to retry
    # the connection
    GOTO :connect
END IF
 
# Once this point is reached ScriptFTP
# will be connected to the server.
# Transfer the files.
GETFILE("*.*")
 
# Close connection
CLOSEHOST

The example above will try to connect to the FTP server indefinitely. Let us add some code in order to make only three attempts:

# This variable will store the connection attempts done.
# It is initially is set to 0.
$attempts=0
 
# This is a label. It marks a point in the script.
# We will use it to return to this point if a
# connection attempt fails.
:connect
 
# Add 1 to the connection attempts counter
$attempts=$attempts+1
 
# Display a message
PRINT("Connecting. Attempt number ".$attempts)
 
# Connect to server. The return value of OPENHOST
# is stored in $result
$result=OPENHOST("myserver.com","me","13579")
 
# Check if $result is different from "OK"
IF($result!="OK")
    # If this is the third attempt stop execution
    IF($attempts==3)
    STOP
ELSE
    PRINT("Cannot connect! Trying again.")
    # Jump to the label :connect to retry
    # the connection
    GOTO :connect
END IF
 
# Once this point is reached ScriptFTP
# is connected to the server.
# Transfer the files.
GETFILE("*.*")
 
# Close connection
CLOSEHOST

The command PUTFILE also outputs a return value. We will evaluate it in order to check whether the upload has been performed successfully:

$webserver="www.whatever.com"
$myuser="me"
$mypassword="13579"
 
OPENHOST($webserver,$myuser,$mypassword)
 
$my_result_put=PUTFILE("*.*")
 
# If PUTFILE returns anything different
# from OK jump to :failed_put
IF($my_result_put!="OK")
    GOTO :failed_put
END IF
 
:allright
PRINT("All right")
 
# Close connection
CLOSEHOST()
 
# Wait 60 seconds
SLEEP(60)
 
# Close ScriptFTP
EXIT
 
:failed_put
PRINT("Error found putting files")
 
# Close connection
CLOSEHOST()
 
# Wait 60 seconds
SLEEP(60)

You can find more examples of error handling in the examples section of the website. The advanced scripts therein cover this issue in detail.