Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

How to check if a file exists in the FTP server

22nd November 2017

Sometimes, it is needed to perform a given action depending on if a file exists in the remote side. In this case, we will show a small example that downloads the file 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


How to create a local folder using the current date

23rd October 2017

Dates can be written in multiple ways, not only because of the language used (English, Spanish, German…) but also within the same language we can write a date in many different ways. For example in English, we can write it as “20 October 2017”, 20/10/2017, 10/20/2017 etc.

In ScriptFTP the most common formats of dates are available in the GETTIME command but sometimes you need to retrieve the date or time components separately (month, day, year, hour…) and build your own expression of a date or time. In this example, it is shown how to build a date expression without spaces and using a three letter month, for example “20Jan2017”. We will then use it to create a local directory.

 

read more …

Tags


How to download a file from the web in ScriptFTP

29th December 2016
ScriptFTP has commands to retrieve files from FTP sites only but it can also download files from the web (HTTP/HTTPS) using an external tool like CURL. You can download it going to this page. The Windows version of CURL is almost at the bottom of that page.
Once you have downloaded it you need to place curl.exe in any folder and call it from ScriptFTP using the EXEC command this way:
# Set the current local directory
LOCALCHDIR("C:\Users\Carlos\Desktop\curl_downloaded_files")
 
# Call curl
EXEC("C:\path_to\curl\curl.exe -O https://www.mydomain.com/thefile.zip")
If you browse the CURL website or run “curl.exe –help” you will notice the big amount of options and switches it has. It is a very versatile tool that not only downloads file from websites, it is also capable of automating form filling, HTTP authentication etc. For example, if the web browser prompts to enter an user name and password you can tell curl to login this way:
# Download a file using HTTP authentication
EXEC("C:\path_to\curl\curl.exe --user johndoe:thepswd -O https://www.mydomain.com/thefile.zip")
Note that HTTP authentication is not the kind of authentication you commonly find when going to web sites like yahoo mail, gmail etc. These are just HTML forms. You can identify HTTP authentication when the browser asks for the login in a very spartan pop-up window on top of the web browser.

read more …

Tags


Handling file dates and time spans

13th December 2016

The ScriptFTP scripting language does not have data types (as most scripting languages) and dealing with anything which is not plain text requires some kind of tricks. This happens with file lists as we have seen before, dates and time spans suffer the same limitation. This post covers how these data types are handled in ScriptFTP.

read more …


Lists in ScriptFTP

24th November 2016

ScriptFTP, as many script languages, does not have data types. This makes the script language and its syntax a lot easier to learn but not as powerful as others. Think of it as being “not so descriptive” and “less complex”. The drawback, in practice, is that you cannot handle things like “lists”,”sets” etc. Everything is a plain text string and you need to deal with this limitation to get the things done.

Regarding to the lists. How can the FTP script handle a list? There is a shortcut:

$my_list = "my_item_number_1|my_item_number_2|my_item_number_3"
 
FOREACH $item_name IN $my_list
     PRINT($item_name)
END FOREACH

Running the script you get:

my_item_number_1
my_item_number_2
my_item_number_3

 

read more …

Tags