ftp automation

ScriptFTP

The professional tool to automate FTP, SFTP, FTPS and schedule FTP batch jobs

The forum is now read only. Please, go to the the main ScriptFTP website if you need help.
Need help writing a script? Have any question about ScriptFTP?
hello all,
For our site i have developed this script below.
I used bits and pieces i found on the forum and with help of the developer of this great tool.

Use it where ever it fit's your needs.
Perhaps you see points for improvement, feel free to add them
Code: Select all# This script does the following # 1. Creates a logfile # 2. Opens the connection to the FTP server #    when connection attempt fails after 100 times it stops the script and send email # 3. Checks if the logfile is from today or yesterday, when yesterday create new logfile # 4. There is a loop that checks if connection is valid, if not then it goes back to point 2 # 5. Check if directory is empty, when empty wait and then retry. When files are waiting, go further # 6. Move the waiting files to a temp folder and create a trigger file. # 7. Upload the TXT files and then move them to a Archive folder #      when failed move to error folder and send email # 8. Upload the TRG files and then delete them # 9. Go back to point 3 ######################################################################################## $attempts=0 ###################################################################################### # FTP server settings ###################################################################################### $ftp_server="<Servername>" $ftp_user="<username>" $ftp_password="<password>" ###################################################################################### # Set here the directories that this script will use ###################################################################################### $remote_dir="remote" $local_dir="c:\local_dir\" $local_dir_temp="c:\local_dir_temp\" $local_dir_Archive="c:\local_dir\Sent" ###################################################################################### # Blat parameters. This is the command line program # used in this example to send emails from ScriptFTP ###################################################################################### $blat_path="d:\ftp_upload\blat\blat.exe" $smtp_server="smtp.server" $smtp_user="" $smtp_password="" $email_from="something@important.com" $email_to="something@important.com" $email_subject1="Subject text 1" $email_subject2="Subject text 2" $email_body1="Body text 1" $email_body2="Body text 2" ###################################################################################### # Mail file creation ###################################################################################### $common_part_1=$blat_path." -server ".$smtp_server." -f ".$email_from." -to ".$email_to." -subject ".'"' $common_part_2=$email_subject1.'"' $common_part_3=$email_subject2.'"' $cmd_line_part_4_1=" -body ".'"'.$email_body1.'"'." -ps ".'"'.$logfile.'"' $cmd_line_part_4_2=" -body ".'"'.$email_body2.'"'." -ps ".'"'.$logfile.'"' # Concatenate the text string to build the complete command lines $blat_cmd_line_1=$common_part_1.$common_part_2.$cmd_line_part_4_1 $blat_cmd_line_2=$common_part_1.$common_part_3.$cmd_line_part_4_2 ###################################################################################### # Log file creation ###################################################################################### :logfile $logpath="c:\logs" $logfile=$logpath."\logfile_".GETDATE(FORMAT3).".txt" PRINT("Create new logfile") SILENT(ON) ######################################################################################## # FTP Connection is established and reports via email when connection is failed ######################################################################################## # Display a message LOGTO($logfile,APPEND) PRINT("_____Connecting_____") :reconnect $resultFTP=OPENHOST($ftp_server,$ftp_user,$ftp_password) $attempts=$attempts+1 IF($resultFTP!="OK")  IF($attempts==100)  PRINT("Cannot connect to FTP server. Sending an email and aborting in 60 seconds.")    EXEC($blat_cmd_line_1)  SLEEP(60)    EXIT(1) ELSE    PRINT("Cannot connect! Trying again.")  GOTO :reconnect     END IF END IF STOPLOG() ######################################################################################## # START POINT FOR THE LOOPING ######################################################################################## :start ######################################################################################## # Check if Logfile is from today, when not, create new ######################################################################################## :checklogdate $current_day=GETDATE(DAY) $file_last_modification_time=GETFILETIME(LOCAL,$logfile) $file_last=TEXTCUT($file_last_modification_time,9,2) IF($file_last==$current_day)  PRINT("Logfile is from today.")  GOTO :startdir ELSE  PRINT("Logfile is from yesterday.")  GOTO :logfile END IF ######################################################################################## # Check if the directory is empty, or if it contains files to process ######################################################################################## :startdir LOGTO($logfile,APPEND) LOCALCHDIR($local_dir) GETLIST($checklist,LOCAL_FILES) $number_of_elements=COUNTELEMENTS($checklist) IF($number_of_elements==0) SLEEP(10) PRINT("No files in folder, wait 10 seconds") STOPLOG() GOTO :start ELSE STOPLOG() GOTO :check END IF STOPLOG() ######################################################################################## # Check if Connection is live ######################################################################################## :check LOGTO($logfile,APPEND) IF(ISCONNECTED())  PRINT("Connection is still valid")  STOPLOG()  GOTO :batch1 ELSE  PRINT("Connection is broken. Reconnecting.")  STOPLOG()  GOTO :reconnect  END IF STOPLOG() ######################################################################################## # Create trigger files for each original file and move the files to a TEMP folder ######################################################################################## :batch1 LOCALCHDIR($local_dir) GETLIST($mylist,LOCAL_FILES,"*.txt") FOREACH $txtfile IN $mylist     EXEC("move ".$txtfile." c:\local_dir\temp\".$txtfile)     $windows_filename=TEXTCUT($txtfile,1,TEXTLENGTH($txtfile)-4)     $trgfilename=$windows_filename.".trg" EXEC("D:\FTP_Upload\scripts\touch.exe c:\local_dir\temp\".$trgfilename) END FOREACH ######################################################################################## # Uploading the original files that are in the TEMP folder # after successfull upload, move the file to the ARCHIVE folder ######################################################################################## :batch2 LOCALCHDIR($local_dir_temp) CHDIR($remote_dir) GETLIST($mytxtlist,LOCAL_FILES,"*.txt") LOGTO($logfile,APPEND) FOREACH $txtfile IN $mytxtlist $result=PUTFILE($txtfile) IF($result=="OK")         EXEC("move ".$txtfile." c:\local_dir\Sent\".$txtfile)             PRINT("Move...... ".$txtfile." to the sent folder") ELSE             EXEC($blat_cmd_line_2)             EXEC("move ".$txtfile." c:\local_dir\errorfiles\".$txtfile)             PRINT("Moved ".$txtfile." to the error folder") END IF END FOREACH STOPLOG() ######################################################################################## # Uploading the trigger files that are in the TEMP folder # after successfull upload, delete all the trg files in 1 batch sequence ######################################################################################## :batch3 GETLIST($mytrglist,LOCAL_FILES,"*.trg") LOGTO($logfile,APPEND) FOREACH $trgfile IN $mytrglist $result2=PUTFILE($trgfile) END FOREACH EXEC("del /F /Q *.trg") STOPLOG() ######################################################################################## GOTO :start  CLOSEHOST EXIT(0)  
Thanks Marty, probably many users find it useful.