check_size_changed.ftp

This script checks every 10 seconds if a remote file has changed its size. If a change is detected the file is downloaded. It also checks for the result of most commands and retry the connection if an error is found.

:start
 
$result=OPENHOST("127.0.0.1","carl","123456")
 
# Check if the connection failed
IF($result!="OK")
 # wait 5 seconds and try again
 SLEEP(5)
 GOTO :start
END IF
 
:check_file_size
 
$file_size=GETFILESIZE(REMOTE,"data.log")
 
 
# Check if GETFILESIZE failed
IF($file_size<0)
 # wait 5 seconds and try again
 SLEEP(5)
 
 # Connection may be still opened
 CLOSEHOST
 
 # Jump to the beginning of the script
 GOTO :start
END IF
 
PRINT("Last size was ".$last_file_size)
PRINT("Read size is ".$file_size)
 
# Check if the file size has changed
IF($last_file_size!=$file_size)
 # the size has changed. download the file
 
 PRINT("File size change detected")
 $result=GETFILE("data.log")
 
 # If the download failed jump to start
 IF($result!="OK")
 # wait 5 seconds and try again
 SLEEP(5)
 
 # Connection may be still opened
 CLOSEHOST
 
 # Jump to the beginning of the script
 GOTO :start
 ELSE
 PRINT("File successfully downloaded")
 $last_file_size=$file_size
 
 # Wait 10 seconds and check if
 # the size has changed again
 SLEEP(10)
 GOTO :check_file_size
 END IF
ELSE
 # The size has not changed.
 # wait 10 seconds and check again
 SLEEP(10)
 GOTO :check_file_size
END IF