Delete Old Files from FTP
How can I need to delete files from FTP which are more than 30 days old ? The Filename format provides the date of the file like 'ABC_10-09-2010 12:09:000.000' Thanks,
November 1st, 2010 3:29pm

you need scripting to do this, in a script you should loop through files in FTP remote folder, and check the date pattern at each file name and check if the file is older than 30 days or not, if it was old you can delete it within the script or fill an array with name of old files to a foreach loop and delete files with a ftp task there. Let me know where you need more details.http://www.rad.pasfu.com
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2010 3:52pm

Thanks alot for the reply. I am quite poor at scripting so will need help. I found following script in internet. Here it can connect to FTP site and delete a single hardcoded file. Now, I want to expand this to delete multiple files with date more than 30 days old. I dont know which function to use to find the file name in the FTP site and use the condition on the filename. Public Sub Main() Try Dim cm As ConnectionManager = Dts.Connections.Add("FTP") cm.Properties( "ServerName").SetValue(cm, "server") cm.Properties( "ServerUserName").SetValue(cm, "username") cm.Properties( "ServerPassword").SetValue(cm, "password") cm.Properties( "ServerPort").SetValue(cm, "21") cm.Properties( "Timeout").SetValue(cm, "0") cm.Properties( "ChunkSize").SetValue(cm, "1000") cm.Properties( "Retries").SetValue(cm, "1") Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing)) ftp.Connect() Dim files(0) As String files(0) = "/dir/subdir/abc_10-09-2010.txt" ftp.DeleteFiles(files) ftp.Close() Catch ex As Exception Dts.TaskResult = ScriptResults.Failure End Try Dts.TaskResult = ScriptResults.Success End Sub End Class
November 1st, 2010 4:36pm

with this code you can get list of files in the directory: Public Sub Main() Try Dim request As System.Net.FtpWebRequest request = System.Net.WebRequest.Create("ftp://remoteurl.com/parentfolder/childfolder/") 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) MessageBox.Show(reader.ReadToEnd()) reader.Close() response.Close() Catch ex As Exception Dts.TaskResult = ScriptResults.Failure End Try Dts.TaskResult = ScriptResults.Success End Sub now you should loop through files and find out what files are older than 30 days , If you can not do this part yourself, let me know what is your filenames looks like? some examples of filenames can be helpful.http://www.rad.pasfu.com
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2010 5:03pm

Thanks alot for help!!!!! I still cannot loop through the file and delete the selected ones. All of my files are Flat Files. Names are in the form of xxx_2010-01-01 12:01:00.000.txt xxx --> any of three characters 2010-01-01 12:01:00.000 --> any date time I need to delete only those files which are 10 days or older Thanks,
November 2nd, 2010 10:00am

Names are in the form of xxx_2010-01-01 12:01:00.000.txt xxx --> any of three characters 2010-01-01 12:01:00.000 --> any date time file name can not have special characters like ( : ) you should now your filenames exactly because you should fetch datepart from it. so let me know correct names of your files.http://www.rad.pasfu.com
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2010 2:50pm

ohh..i am sorry...i misread it.. names of the files are in the format of : xxx_2010-09-21 03-00-21.txt Thanks,
November 2nd, 2010 3:26pm

this is whole story in the script task: 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 I tested and it works correctly.http://www.rad.pasfu.com
Free Windows Admin Tool Kit Click here and download it now
November 2nd, 2010 4:20pm

IT WORKS !!!! YOU ARE VERY HELPFUL!!!!!!!!!! THANKS ALOT!!!
November 2nd, 2010 4:26pm

You can also write your own SearchCondition class named YourSearchCondition that converts the file name to datetime and check whether it's over 30 days using DeleteDirectory method. The code example would look like: // Create a new instance. Ftp client = new Ftp(); // Connect to the FTP server. client.Connect( "myserver" ); // Authenticate. client.Authenticate( "userName" , "password" ); // ... // Delete all remote files that are 30 days older recursively. client.DeleteDirectory( "/temp" , true, new YourSearchCondition() ); // ... // Disconnect. client.Disconnect(); the example use the Ultimate FTP component .
Free Windows Admin Tool Kit Click here and download it now
March 2nd, 2011 11:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics