How to check if a file exists in the FTP server

22nd November 2017

This blog post shows how to check if a file exists in the FTP server. The shown example tries to download a file only if it exists but if it does not exist it creates an empty file locally. The filename here is EXAMPLE.txt but it can be any other name.

Beware that this script does not go through all the directory tree in the FTP server searching for the file. It only checks one directory (/remotedir) and does not go through subdirectories. This is done getting the file list first with GETLIST and then we use FOREACH to compare every file name we got in the previous step with the text string “EXAMPLE.txt”:

 

# Connect to the server
# Get the list of remote files
 GETLIST($my_remote_files, REMOTE_FILES)
 
# We will save in this variable whether or not the file exists
 $exists="no"
 
# Go one by one through the list of remote files
 FOREACH $file IN $my_remote_files
     IF($file=="EXAMPLE.txt")
          $exists="yes"
     END IF
 END FOREACH

read more …

Tags


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:

read more …

Tags