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?
I have made great progress but am stuck with one issue.

I need to delete remote folders with certain conditions. Either can be true, meaning either will work for me.

Delete remote directories that are empty.

or

Delete remote directories that are > than seven days old.

Any help would be great.

Thank you
An example is worth thousands of words:



FTP Script
  1.  
  2. # This script deletes any remote directory in the first level of directories of the FTP site that is
  3. # empty or is more than a week old. Use with caution.
  4.  
  5. OPENHOST("127.0.0.1","test","1234")
  6.  
  7. # Get the list of directories in the FTP site (only directories in the root folder)
  8. GETLIST($list,REMOTE_DIRECTORIES)
  9.  
  10. # For each directory
  11. FOREACH $dirname IN $list
  12.    
  13.     # Get the last modification time
  14.     $directory_time=GETFILETIME(REMOTE,$dirname)
  15.  
  16.     # Calculate how old is that directory
  17.     $seconds_old = GETTIME() - $directory_time
  18.     PRINT($dirname." is ".$seconds_old." seconds old")
  19.    
  20.     # is it older than a week?
  21.     IF($seconds_old>(7*24*60*60))
  22.         $is_older_than_a_week = "TRUE" 
  23.     ELSE
  24.         $is_older_than_a_week = "FALSE"
  25.     END IF
  26.  
  27.     # Change current remote path to that directory
  28.     CHDIR($dirname)
  29.  
  30.     # Get the listing of files and subdirectories
  31.     $result1=GETLIST($list_files_inside_dir,REMOTE_FILES)
  32.     $result2=GETLIST($list_directories_inside_dir,REMOTE_DIRECTORIES)
  33.     $number_of_files = COUNTELEMENTS($list_files_inside_dir)
  34.     $number_of_directories = COUNTELEMENTS($list_DIRECTORIES_inside_dir)
  35.  
  36.     # If we have successfully retrieved the file and directory listings
  37.     # and the number of items os zero...
  38.     IF($result1 =="OK" AND $result2=="OK" AND ($number_of_files + $number_of_directories)==0)
  39.         $is_empty = "TRUE"
  40.         PRINT($dirname." is empty")
  41.     ELSE
  42.         $is_empty = "FALSE"
  43.         PRINT($dirname." is NOT empty")
  44.     END IF
  45.  
  46.     # return to the FTP site root folder
  47.     CHDIR("..")
  48.  
  49.     # If the directory is older than a week or is empty proceed to remove it
  50.     IF($is_older_than_a_week OR $is_empty)
  51.         # Uncomment this if you are sure what you are doing
  52.         # RMDIR($dirname)
  53.     END IF
  54.    
  55.