download_and_delete_2.ftp
This script is a real case. The customer had a remote site that contained 50 directories, each directory contained 2 subdirectories and each subdirectory contained files. That files had to be downloaded and removed from the remote site only if the download was succesful.
# Remote directory where the 50 directories are located $remote_directory="/" # Local directory where the files are downloaded $local_directory="D:\test" # Connect to server, put your own settings here $result=OPENHOST("127.0.0.1","carl","123456") # If connection failed stop IF($result!="OK") STOP END IF # Change the current local directory. $result=LOCALCHDIR($local_directory) # If LOCALCHDIR failed stop the script IF($result!="OK") STOP END IF # Change the current remote directory. $result=CHDIR($remote_directory) # If CHDIR failed stop the script IF($result!="OK") STOP END IF # Once reached this point we are connected and ready # to browse the remote 50 directories with 2 subdirectories # in each one and a bunch of files in them ready for download. # Get the remote directory listing # it should retrieve 50 items. $result=GETLIST($list,REMOTE_DIRECTORIES) # If GETLIST failed stop the script IF($result!="OK") STOP END IF # For each directory in $list... FOREACH $directory IN $list # Change current remote directory to that # subdirectory CHDIR($directory) # Create a local directory with the same name # and change current local directory to it. # LOCALMKDIR may fail because it already exists LOCALMKDIR($directory) LOCALCHDIR($directory) # Get the subdirectory listing. It should get # two items GETLIST($list2,REMOTE_DIRECTORIES) FOREACH $subdirectory IN $list2 # Change current remote directory to that # subdirectory CHDIR($subdirectory) # Create the local subdirectory with the same name # and change current local directory to it. # LOCALMKDIR may fail because it already exists LOCALMKDIR($subdirectory) LOCALCHDIR($subdirectory) # Get the file listing GETLIST($list3,REMOTE_FILES) FOREACH $file IN $list3 # Download the file $result=GETFILE($file) # If the file has been succesfully downloaded # delete the remote copy. If not stop the script. IF($result=="OK") DELETEFILE($file) ELSE STOP END IF END FOREACH # Return to parent directory CHDIR("..") LOCALCHDIR("..") END FOREACH # Return to parent directory CHDIR("..") LOCALCHDIR("..") END FOREACH