Script

FTP

The professional tool to automate FTP, SFTP, FTPS

and schedule FTP batch jobs

MENUMENU

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:

# Connect to the FTP site
OPENHOST("ftp.myhost.com","my_username_here","the_password")
 
# Change the current local working directory. We will download the files to here
LOCALCHDIR("C:\Users\Carlos\Desktop\files on the FTP created in the last 48h")
 
# Change the current remote folder to where the files we want to download are located
CHDIR("/the_remote_folder")
 
# Get the listing of the remote folder
GETLIST($remote_file_list,REMOTE_FILES)
 
# We have to calculate how many seconds
# are 48 hours to compare against the file date later
$two_days_in_seconds = 48 * 60 * 60
 
# A fancy message from the script universe to the real world
PRINT("two days in seconds is ". $two_days_in_seconds ." seconds")
 
# We store the current date and time in this variable
$now = GETTIME()
 
# for each file in the remote folder...
FOREACH $file IN $remote_file_list
 
	# ask the server what is the time of that file
	$file_time=GETFILETIME(REMOTE,$file)
 
	# the file is this amount of seconds older
	$seconds_old = $now - $file_time
 
	# Another message
	PRINT("The file ". $file . " is ". $seconds_old . " seconds old")
 
	# And this is where we check if this file is older or not than 48 hours
	IF($seconds_old < $two_days_in_seconds)
		# Download the file
		GETFILE($file)
	END IF
END FOREACH
 
# Say goodbye to the FTP server and close the connection
CLOSEHOST

 

And in the following screenshot you can see the output of the script. Two files were in the FTP site. The first one was older than two days so we ignore it, and the other one was created about 5 minutes before the script was run so we download it: