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?
Just to inquire, how will ScriptFTP handle the files if there is a file with the same name?

For example, using the GETLIST / FOREACH and GETFILE combination, b4 i GETFILE the files from the FTP server, i would like to check if my target directory has the same filename. If the filename exist, to rename the file and save to target.

Is this workable?
Hello,

Yes, it can be done using the FOREACH command. Take a look at this example I've written for you:cc
FTP Script
  1. # Settings
  2. $ftpserver="127.0.0.1"
  3. $ftpuser="carl"
  4. $password="123456"
  5.  
  6. # The files will be downloaded here
  7. $downloaded_files_directory="D:\downloaded_files"
  8.  
  9.  
  10. # For each file downloaded we check first if we
  11. # already have a file with the same name in the
  12. # local directory $downloaded_files_directory
  13. # If it happens the local file is moved to
  14. # this directory:
  15. $target_directory="D:\target"
  16.  
  17.  
  18. # Connect to FTP server
  19. OPENHOST($ftpserver,$ftpuser,$password)
  20.  
  21. # Change current local directory
  22. LOCALCHDIR($downloaded_files_directory)
  23.  
  24. # Retrieve the remote file listing and store it in $remote_file_list
  25. GETLIST($remote_file_list,REMOTE_FILES)
  26.  
  27. # Retrieve the local file listing and store it in $local_file_list
  28. GETLIST($local_file_list,LOCAL_FILES)
  29.  
  30.  
  31. # For each element in the file list do the following
  32. FOREACH $remote_file IN $remote_file_list
  33.  
  34.     # Check if the remote file already exists
  35.     # in the local directory
  36.     $exists="FALSE"
  37.     FOREACH $local_file IN $local_file_list
  38.         IF ($local_file == $remote_file )
  39.             $exists="TRUE"
  40.         END IF
  41.     END FOREACH
  42.  
  43.     # If it already exists move the file to
  44.     # the target directory and append the time
  45.     # to the file name.
  46.     IF($exists)
  47.         $current_time=GETDATE(FORMAT1)
  48.         EXEC('move "'.$remote_file.'" "'.$target_directory.'\'.$current_time.'-'.$remote_file.'"')
  49.     END IF
  50.  
  51.     # Download the file
  52.     GETFILE($remote_file)
  53.  
  54.  
  55. # Close the connection with the FTP server
  56.  
Thanks a lot :)