Archives
- July 2019
- January 2019
- November 2018
- August 2018
- July 2018
- February 2018
- November 2017
- October 2017
- July 2017
- June 2017
- May 2017
- April 2017
- January 2017
- December 2016
- November 2016
- August 2016
- January 2015
- December 2014
- March 2014
- April 2013
- December 2010
- November 2009
- September 2009
- June 2009
- March 2009
- February 2009
- November 2008
- October 2008
- August 2008
- July 2008
- January 2008
How to get the latest file out of each folder
6th November 2017
Recently a user has asked via the website chat (the icon at the lower right corner) an interesting question:
I am looking for scriptable FTP to do the following… I have an FTP location with many folders(about 500.) I would like to get the latest file out of each folder. Or better yet, get the latest file out of a list of folders. Can your program do this recursive FTP?
The answer is yes. It is possible to get the latest file out of a list of folders. It is not a simple script but it is definitely possible. The steps needed are the following:
- Store the list of remote folders where you want to look in a variable. Each item of the list is separated by the | character.
- Use the FOREACH loop to run a set of FTP commands for each folder defined in the previous step
- Use the command GETLIST to retrieve the list of files in a folder.
- Use again the FOREACH loop to run a set of FTP commands for each file.
- Retrieve the last modification time of the remote file using GETFILETIME and save it in a variable.
- If it is the most recent modified time we have seen store also the path of the file.
- Once all the files are processed go the next folder of the list.
- When we have reached the end of the folder list download the file using the path we have saved in a variable.
The only drawback of this script is that it only handles the files in a folder directly, not files stored in subfolders. In other words, it does not go through a tree of subdirectories. But there is a workaround to this limitation: You can add the subfolder to the folder list as /myfolder/mysubfolder.
The full script and its output is:
# Connect to FTP server OPENHOST("127.0.0.1","test","1234") # Set the list of folders to look into $folders = "my folder 1|my folder 2|my folder 3| my folder 3/my subfolder" # Save the currenct remote folder path for later $root_folder = CWDIR() # Here we will save the most recent file modified time found $most_recent_modification_time ="" # And here the most recent file path $most_recent_file_path ="" # For each folder in the previously defined list of folders FOREACH $folder IN $folders # Show a message PRINT("Checking files in ".$folder) # Go to that folder CHDIR($folder) # Get the list of files GETLIST($file_list,REMOTE_FILES) # For each file FOREACH $file IN $file_list # Get the last time that file was modified $last_modified_time = GETFILETIME(REMOTE,$file) # A message to track what the script is doing PRINT("Last modified time of ".$file." is ".$last_modified_time) # Is this modification time the most recent we have found? IF($most_recent_modification_time=="" OR $most_recent_modification_time<$last_modified_time) $most_recent_modification_time=$last_modified_time $most_recent_file_path = CWDIR()."/".$file END IF END FOREACH # return to parent folder CHDIR($root_folder) END FOREACH # Download the most recent file among the set of folders defined at the top GETFILE($most_recent_file_path) # Close connection to the FTP server CLOSEHOST