Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

  sync_loop.ftp

This script does an infinite loop and keeps a local directory synchronized from a FTP site. If an error is found it waits, shows a message and closes ScriptFTP.

The error handling mechanism in this example avoids the use of variables to store the command returned values. It directly inserts the command within the IF sentence. It’s not as clear as storing the returned value in a variable an then evaluate but it also works.

Note also that the commands between WHILE and END WHILE will be executed again and again because the expression put between the WHILE brackets is always “TRUE”.

# Write your own settings here
$host="172.16.0.4"
$user="carl"
$password="123456"
 
# Connect to FTP server
IF(OPENHOST($host,$user,$password)!="OK")
 PRINT("Connection error. Exiting")
 
 # Wait for 10 seconds
 SLEEP("10")
 
 # Close ScriptFTP. Exit code 1
 EXIT(1)
END IF
 
#do an infinite loop
WHILE("TRUE")
 IF(SYNC("C:\local_dir","/myremotedir",DOWNLOAD)!="OK")
 PRINT("Synchronization error. Exiting")
 
 # Wait for 10 seconds
 SLEEP("10")
 
 # Close ScriptFTP. Exit code 2
 EXIT(2)
 ELSE
 PRINT("All files downloaded. Waiting 10 seconds")
 SLEEP(10)
 END IF
END WHILE