Script

FTP

The professional tool to automate FTP, SFTP, FTPS and Batch FTP

ScriptFTP has been designed for unattended operation and usually you will know what has happened during a script run just by taking a look at the ScriptFTP window. Additionally, you may log a script’s output messages to a text file and even have it send emails to you which contain information about the run. This section is dedicated to this topic.

In ScriptFTP there is no built-in command for sending emails because it is not needed. We will rather use the EXEC command to call an external command-line program that will send the email. The following examples use Blat, a free and tremendously useful command-line mail program for all kinds of batch jobs. However, you can use any other mail program just by adjusting the EXEC call.

The following example script will send an email if it cannot connect to the FTP server or if the file synchronization fails. Note that we first setup the way how Blat will be invoked. We construct two different commands for calling Blat, one for each type of error, since the user should tell from the email what happened exactly and not be confused by a generic error message.

# FTP server settings
$ftp_server="ftp.myserver.com"
$ftp_user="myuser"
$ftp_password="mypass"
 
# Set the directories that this script
# will synchronize.
$remote_dir_to_synchronize="/remotedir/"
$local_dir_to_synchronize="C:\MyDir"
 
# Blat parameters. This is the command line program
# used in this example to send emails from within ScriptFTP
 
$blat_path="C:\blat\blat.exe"
 
$smtp_server="smtp.myserver.com"
$smtp_user="myuser_at_myserver.com"
$smtp_password="mypassword"
 
$email_from="scriptftp@myserver.com"
$email_to="me@myserver"
$email_subject="ScriptFTP error message"
$email_body1="Could not connect to FTP server"
$email_body2="Could not syncronize files"
 
# Build the log file path by retrieving Windows' temp path from
# the system environment variable TEMP and appending the current
# date to the file name "logfile-". For example:
# C:\windows\temp\logfile-20070424.txt
$log_file_path=GETENV("TEMP")."\logfile-".GETDATE(FORMAT3).".txt"
 
# Start logging the script's output
LOGTO($log_file_path)
 
# The blat command for sending an email is supposed
# to look like this (without the line breaks):
#
# C:\blat\blat.exe
# -server smtp.myserver.com
# -u myuser_at_myserver.com
# -pw mypassword
# -f scriptftp@myserver.com
# -to me@myserver
# -subject "ScriptFTP error message"
# -body "Error messsage here"
# -ps "C:\Users\Carlos\AppData\Local\Temp\logfile-20070427.txt"
#
# As there are two different kinds of emails depending
# on the errors that may have happened we need to build
# two different blat commands
#
# Both have a common part which consists of the variables
# $common_part_1, $common_part_2 and $common_part_3.
# The fourth part contains the different body and
# they are called $cmd_line_part_4_1 and $cmd_line_part_4_2
#
$common_part_1=$blat_path." -server ".$smtp_server." -u ".$smtp_user." -pw "
$common_part_2=$smtp_password." -f ".$email_from." -to ".$email_to." -subject ".'"'
$common_part_3=$email_subject.'"'
 
$cmd_line_part_4_1=" -body ".'"'.$email_body1.'"'." -ps ".'"'.$log_file_path.'"'
$cmd_line_part_4_2=" -body ".'"'.$email_body2.'"'." -ps ".'"'.$log_file_path.'"'
 
# Concatenate the text strings to build the complete commands
$blat_cmd_line_1=$common_part_1.$common_part_2.$common_part_3.$cmd_line_part_4_1
$blat_cmd_line_2=$common_part_1.$common_part_2.$common_part_3.$cmd_line_part_4_2
 
# Connect to the FTP server
$result=OPENHOST($ftp_server,$ftp_user,$ftp_password)
IF($result!="OK")
     PRINT("Cannot connect to FTP server. Sending an email and aborting in 5 seconds.")
     EXEC($blat_cmd_line_1)
     SLEEP(5)
     EXIT(1)
END IF
 
# Synchronize the files
$result=SYNC($remote_dir_to_synchronize,$remote_dir_to_synchronize,UPLOAD)
IF($result!="OK")
     PRINT("Cannot syncronize. Sending an email and aborting in 5 seconds.")
     EXEC($blat_cmd_line_2)
     SLEEP(5)
     EXIT(2)
     END IF
CLOSEHOST
 
EXIT(0)