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
Uploading only the files created or modified in the last 7 days
The following script shows how to upload to the FTP server the files from the last 7 days. If a file is more than 7 days old it will be ignored.
Note that this example does not work with subfolders.
How to download the files created in the last 48 hours
Date and time operations in ScriptFTP are something that we often get asked in the tech support email. ScriptFTP stores time ranges in seconds and this is sometimes counter-intuitive if you want to do date and time operations like checking how old a file is. Fortunately, this type-free approach in the ScriptFTP language also makes the syntax easier to understand.
Well, let’s go to the actual topic of this blog post: How you can check if a file in the FTP site was created in the last 48 hours and if so download it. Here is the script. The comments in it are very self-explanative:
How to delete all the sub-directories in a directory
Recently we received a question from a customer that was using a set of IP cameras and a central FTP server where the cameras stored their recorded videos. As the cameras generate a lot of data they wanted to schedule an FTP script that deletes all the sub-folders of a given folder.
The script is pretty straightforward, all it is needed is to use GETLIST to retrieve the list of sub-directories, then use FOREACH and RMDIR to delete each one:
How to check if a file exists in the FTP server
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
How to get the latest file out of each folder
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.
How to create a local folder using the current date
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.
How to download a file from the web in ScriptFTP
# 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")
# Download a file using HTTP authentication EXEC("C:\path_to\curl\curl.exe --user johndoe:thepswd -O https://www.mydomain.com/thefile.zip")
Handling file dates and time spans
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.
Lists in ScriptFTP
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