Few days ago, I encounter a request which asks for delete files on remote ftp folder which are older than 10 days with SSIS tasks. Suppose date of each file is embeded in the file name like this: zzz_2010-10-25 03-00-22.txt
As you may know , using Foreach loop with configuring enumerator as file enumerator will not work on remote ftp folder. a work around can be this: copy all remote files to a local directory and then find file names which are old, and then in a foreach loop delete these files with ftp task. This is a doable solution BUT copying all files from remote ftp to local directory is not wise solution because there maybe thousand files on remote folder and copying them to local directory will take long time.
So there is a Good Solution for this problem which needs scripting, we can use the Powerful Script Task for this purpose with few lines of .NET code to loop through remote files and find old files and delete them.
This is the script I used to handle this problem with the help of System.Net.WebRequest :
Public Sub Main()
Try
Dim request As System.Net.FtpWebRequest
request = System.Net.WebRequest.Create("ftp://remoteurl.com/remotefolder/")
request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
request.Credentials = New System.Net.NetworkCredential("username", "password")
Dim response As System.Net.FtpWebResponse
response = request.GetResponse()
Dim responseStream As System.IO.Stream
responseStream = response.GetResponseStream()
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
While Not reader.EndOfStream
Dim strTemp As String = reader.ReadLine
Dim fileName = strTemp.Substring(strTemp.IndexOf("_") – 3)
Dim theDate As DateTime = DateTime.ParseExact(fileName.Substring(4, 10), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)
If (theDate.Date < DateTime.Now.AddDays(-10).Date) Then
request = System.Net.WebRequest.Create("ftp://remoteurl.com/remotefolder/"+fileName)
request.Method = System.Net.WebRequestMethods.Ftp.DeleteFile
request.Credentials = New System.Net.NetworkCredential("username", "password")
response = request.GetResponse()
End If
End While
reader.Close()
response.Close()
Catch ex As Exception
Dts.TaskResult = ScriptResults.Failure
End Try
Dts.TaskResult = ScriptResults.Success
End Sub