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?
Hi

I have just purchased ScriptFTP so this is all new to me

What I would like to do is this

On my FTP Server I have the following file structure - I have a top level folder for the year then subfolders for the month and day so for example files for the 30 March would be in \2018\03\30

What I need is a script to delete any files, subfolders etc that are older than two days i.e any folders such as \2018\ 03\ 21 \2018\ 03\ 20 and so on.

Also come 2019 I would want to delete the 2018 folder too

I am not interested in keeping the files so just want them deleted . I aim to run this script once a day via the Task Scheduler. Is there some kind person who could help with this

Many thanks

Graham
Hi Graham,

Here you have a script that does what you describe. It is a but complex to understand as it contains 3 "FOR EACH" loops one inside the other.

Note that it only deletes the "day" directory, or let's say, the "leaf" of the directory tree. It does not delete the month or the year directories, just in case. It can be modified to do so however.

Below you can also find some screenshots to explain everything.


FTP Script
  1. # Connect to the FTP server, enter your own settings here
  2. OPENHOST("127.0.0.1","the_user_name","the_password")
  3.  
  4. # Get the list of folders in the FTP root directory
  5. # they must be plain year numbers
  6. GETLIST($year_list,REMOTE_DIRECTORIES)
  7.  
  8. # For each year
  9. FOREACH $year IN $year_list
  10.    
  11.     # Open this year fodler
  12.     CHDIR($year)
  13.    
  14.     # Retrieve the list of months
  15.     GETLIST($month_list,REMOTE_DIRECTORIES)
  16.    
  17.     # For each month
  18.     FOREACH $month IN $month_list
  19.        
  20.         # Open the month folder
  21.         CHDIR($month)
  22.  
  23.         # Get the list of days
  24.         GETLIST($day_list,REMOTE_DIRECTORIES)
  25.  
  26.         # For each day
  27.         FOREACH $day IN $day_list
  28.  
  29.             # Show a line in the ScriptFTP screen. Just to show where we are           
  30.             PRINT("checking ".$year."/".$month."/".$day)
  31.  
  32.             # We build a text string with the current day in the format
  33.             # ScriptFTP understands        
  34.             $folder_date_time=$year."_".$month."_".$day."-12_00_00"
  35.  
  36.             # How old is the current folder?
  37.             $seconds_old=GETTIME()-$folder_date_time
  38.  
  39.             # Well, we have the result in seconds, show it
  40.             PRINT("The folder is ".$seconds_old." seconds old")
  41.  
  42.             # Is the folder more than 2 days old (172800 seconds)
  43.             IF((GETTIME()-$folder_date_time)>172800)
  44.                 PRINT("The folder is more than 2 days old")
  45.                 # Delete the day folder and all the files and subdirs that go inside
  46.                 RMDIR($day)
  47.             ELSE
  48.                 PRINT("The folder is less than 2 days old")
  49.             END IF
  50.         END FOREACH
  51.  
  52.         # return to parent folder
  53.         CHDIR("..")
  54.     END FOREACH
  55.  
  56.     # return to parent folder
  57.     CHDIR("..")
  58.  
  59. # We are done! Let's be polite, close the connection before finishing
Attachments
with_silent_output.png
with_silent_output.png (16.96KiB)Viewed 3897 times
with_normal_output.png
with_normal_output.png (38.07KiB)Viewed 3897 times
before_running.png
before_running.png (2.27KiB)Viewed 3897 times
after_running.png
after_running.png (2.19KiB)Viewed 3897 times
Hi

That is brilliant - thanks so much

I would like it to delete the month once we are into a new month plus two days and then the year at year end plus two days

What would I need to change for it to do this

Kind Regards

Graham