FtpDeleteFilesOtherThanTopNMostRecent

Top  Previous  Next

d

The FtpDeleteFilesOtherThanTopNMostRecent command allows you to delete all files in a specified location that match a specified filter apart from a specified number of files using FTP or SFTP transfer protocols.

 

Command Syntax

 

FtpDeleteFilesOtherThanTopNMostRecent:[RemoteDirectory],[FileFilters],[TopNCount],[Host]],[Port],[UserName],[Password,[Protocol]

 

The colon after the command and the commas between parameters are compulsory. Any additional spaces or characters will cause an error to be thrown. The command itself is not case sensitive, but parameters may be depending on the operating system and locale.

 

Parameters:

 

RemoteDirectory: The fully qualified identifying the directory containing the file(s) to delete. This value must be enclosed in quotes if there are any spaces in the the path

FileFilters: The file filter(s) identifying the file(s) to delete, separated by Pipe characters if there are multiple filters. This value must be enclosed in quotes if more than one filter is specified or there are any spaces in any of the the filters. The value can also be passed as a pair of quotation marks indicating an empty string, in which case it will be treated as the all files filter (*.*).

TopNCount: The number of files to retain

Host:The FTP or SFTP server to receive the Delete command

Port: The port on which the server will communicate

UserName: The username for the server

Password: The password for the server

Protocol: A string representing a valid transport protocol, currently one of the following values: FTP, FTPS, SFTP

 

All parameters are compulsory. If any parameter is missing or invalid an error will be thrown.

 

Usages

 

There are many reasons why this command is likely to be useful, but any remote folder containing any set of files where the file name has a lot in common across a list (typically the file name will have a common start or ending with one or more variable elements) is a prime candidate. It can also be used to delete all but the most recent files of a particular type.

 

Filters

 

Date based file names such as 'Backup 2024-07-08.zip' : set the filter to 'Backup*.zip'

Version based file names such as 'My Software Installer 2.0.2.0.zip' : set the filter to  'My Software Installer*.zip'

File Extension based file names such as 'test.zip' : set the filter to '*.zip'

 

Example:

 

;Deleting all zip and txt files in a remote folder except for the three most recent of each.

FtpDeleteFilesOtherThanTopNMostRecent:"/home/www/test/files","*.zip|*.txt",3,my.server.com,22,MyUserName,mypassword,SFTP

 

See Also: FtpDeleteFile FtpDeleteFilesOlderThanSpecifiedDate FtpDownload FtpUpload