Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

MENU MENU
Missing any feature or command? Post your ideas here. Suggestions are welcome.
I haven't found an easy way to delete remote files based on date.

My scenario is this:
I have several webcameras that stores stillimages on my server every minute. I need to keep these images for about a week before I delete them. My cameras may create a filename which includes a counter and when reaching a fixed number, it starts at zero and overwrites the oldest images. So far so good but for this to work, all images need to be in the same folder and this makes it a bit heavy to work with. The cameras can create files and folders based on date and time but then I loose the automatic overwrite and my server fills up with images I no longer need.

What I'm looking for is a function to delete all files older than one week, preferrably where I point to one folder and the deletion proces run thru this folder and sub-folders, deleting files meeting the cirteria of one week old or older. Any chance for something like this in ScriptFTP?
Hello SveinHa,
(...) deleting files meeting the cirteria of one week old or older. Any chance for something like this in ScriptFTP?
A command to retrieve file properties such as size or modification time will be added soon to ScriptFTP. Probably this week. Then you can use the retrieved date to compare against the current date and delete the files older than a week.
Thanks, looking forward to it :)
How is this one going?
Hello Sveinha,

It was finally implemented, see this help topic:

http://www.scriptftp.com/reference.php?go=topic590

Remember to download the last version from the downloads section.

You can delete the files older than a week this way:
FTP Script
  1. $current_date_time=GETDATE(FORMAT0)
  2. PRINT("Current date and time is ".$current_date_time)
  3.  
  4. $a_week_ago=$current_date_time-(7*24*60*60)
  5. PRINT("A week ago was ".$a_week_ago.". Every file older than this will be deleted from the FTP server")
  6.  
  7. # Retrieve the remote file list
  8. GETLIST($remote_file_list,REMOTE_FILES)
  9.  
  10. # For each file in the current remote directory
  11. # check if it is older than a week.
  12. # If so delete it
  13. FOREACH $remote_file IN $remote_file_list
  14.     $file_last_modification_time=GETFILETIME(REMOTE,$remote_file)
  15.     IF($file_last_modification_time<$a_week_ago)
  16.         DELETEFILE($remote_file)
  17.     END IF
If you want to go through folders and subfolders you have to do it manually copying this example to your script as many times as directories you need to process. Use CHDIR for changing the current directory.
Hi.

Thanks for the script sample. I have done some testing and found that doing a RMDIR with a directoryname automatically created works at least 10 times faster than deleting on a file-by-file basis. In my case, I achieve what I want by removing one directory including subdirs every day.

My current script
FTP Script
  1. $current_date_time=GETDATE(FORMAT0)
  2. PRINT("Current date and time is ".$current_date_time)
  3. $a_week_ago=$current_date_time-(8*24*60*60)
  4. PRINT("A week ago was ".$a_week_ago.".")
  5. $LastWeekDir="/aaa/bbb/ccc/".TEXTCUT($a_week_ago,1,4).TEXTCUT($a_week_ago,6,2).TEXTCUT($a_week_ago,9,2)
  6. RMDIR($LastWeekDir)

BTW: When manually deleting these directories (one dir, 3 subdirs containing a total of 4320 files) using FileZilla, the process is another 10 times faster than ScriptFTP:RMDIR
Thanks for the feedback. Yes, the ScriptFTP speed is something that I have to improve a bit.
Good Day,

How can we improve the script to do sub directories as well?
It can be possible if you have one or two levels of subdirectories. Otherwise it gets too complex. For one level it would be:
FTP Script
  1. OPENHOST("ftp.mytestserver.com","myuser","1234")
  2.  
  3. # Retrieve the remote directory list (first level only)
  4. GETLIST($remote_directory_list,REMOTE_DIRECTORIES)
  5.  
  6. # for each directory..
  7. FOREACH $remote_directory IN $remote_directory_list
  8.  
  9.     # Go to that dir
  10.     CHDIR($remote_directory)
  11.  
  12.     # Retrieve the remote file list in the current dir
  13.     GETLIST($remote_file_list,REMOTE_FILES)
  14.  
  15.     # For each file in the current remote directory
  16.     # check if it is older than a week.
  17.     # If so delete it
  18.     FOREACH $remote_file IN $remote_file_list
  19.         $file_last_modification_time=GETFILETIME(REMOTE,$remote_file)
  20.         IF($file_last_modification_time<$a_week_ago)
  21.             DELETEFILE($remote_file)
  22.         END IF
  23.     END FOREACH
  24.  
  25.     # Go back to parent dir
  26.     CHDIR("..")
  27.  
  28.