Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

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.

As we have only text strings to store a date, ScriptFTP uses a given text format. It is the following:

YYYY_MM_DD-hh_mm_ss

This means that the December the 1st 2016 at 8:30 pm is written this way:

2016_12_01-20_30_00

If you use another format ScriptFTP will think it is not a date and any operation done with this variable will fail. As if you try to sum “abc” and “efd”. The result will be zero.

And what can you do with dates? you can, for example, determine if a given date is earlier than another:

$date1="2016_12_01-20_30_00"
$date2="2015_03_01-21_32_16"
 
IF($date1>$date2)
 PRINT("date1 is more recent than date2")
ELSE
 PRINT("date2 is more recent than date1")
END IF

 

The result of the script is the following (as you probably have guessed):

date1 is more recent than date2

 

And you can even calculate the time difference between two dates

$date1="2016_12_01-20_30_00"
$date2="2015_03_01-21_32_16"
 
$my_time_difference = $date1 - $date2
PRINT("there are ".$my_time_difference." seconds between date1 and date2")

The result is:

there are 55378664 seconds between date1 and date2

 

As you can see ScriptFTP handles time differences in seconds.

All these date/time operations (and others not shown here) are mostly used to compare file modification times against the current time or another file. The commands GETFILETIME and GETTIME (get current time) are used a lot in this context.

For example, if we want to delete from the FTP server the files older than a week:

 

# Substract a week (in seconds) from the current date,
# assign the result to another variable
$a_week_ago=$current_date_time-(7*24*60*60)
 
PRINT("A week ago was ".$a_week_ago)
PRINT("Every file older than this will be deleted from the FTP server")
 
# Request the remote file list
GETLIST($remote_file_list,REMOTE_FILES)
 
# For each file in the current remote directory
# check if it is older than a week. If so delete it
FOREACH $remote_file IN $remote_file_list
 $file_last_modification_time=GETFILETIME(REMOTE,$remote_file)
 IF($file_last_modification_time < $a_week_ago)
     DELETEFILE($remote_file)
 END IF
END FOREACH