Delete files older than 3 days using SSIS tasks
I want to delete all files in a given folder that are older than 3 days from today's date. I tried using a "Foreach Loop container" with a"File System" task inside it but found I couldn't access any file properties such as the file creationdate. Am I using the wrong task forthis job? TIA, Barkingdog P.S. History of the problem: I used the sql 2005 Database maintenance program to setup our database backup jobs. One day I noticed that the free space on the drive where we keep the dumps had grown small. I foundthat we had at least 4 weeks of dumps in there, not the 3 days I wanted to keep! I lookedbut could not find the "delete file" optionin the SSIS package generated by the Database Maintenance Wizard. No wonder the files were piling up.
August 11th, 2006 6:13am

if i'm not mistaken, you need to use either a script task or custom task...
Free Windows Admin Tool Kit Click here and download it now
August 11th, 2006 10:17am

Yes, that's what I finally did, thank you. BUT I am surprised that this feature -- which was in sql 2000, is apparently missing from the Mantenance plan in sql 2005. This is a ridiculous shortcoming, unless one likes old backups to accumulate and take all free disk space!. Yelp, yelp, yelp ............ ;) Barkingdog
August 14th, 2006 5:26am

If you need a feature then request it at Microsoft Connect. -Jamie
Free Windows Admin Tool Kit Click here and download it now
August 14th, 2006 11:46am

I will. Barkingdog
August 14th, 2006 9:58pm

Would be a great addition to the file enumerator. Basically provide for more robust and flexible enumeration by date, file attributes etc. I don't think I'm the only one to have thought of this. Certainly useful. Kirk HaseldenAuthor "SQL Server Integration Services"
Free Windows Admin Tool Kit Click here and download it now
August 15th, 2006 8:55am

Good idea. For now I just used this script in a Script task ... Dim fi As System.IO.FileSystemInfo fi = My.Computer.FileSystem.GetFileInfo("c:\temp.txt") If (Now.DayOfYear - fi.CreationTime.DayOfYear) > 3 Then 'fi.Delete() End If
August 15th, 2006 5:45pm

KirkHaselden wrote: Would be a great addition to the file enumerator. Basically provide for more robust and flexible enumeration by date, file attributes etc. I don't think I'm the only one to have thought of this. Certainly useful. Kirk HaseldenAuthor "SQL Server Integration Services" Way ahead of you ;) https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=179264 -Jamie
Free Windows Admin Tool Kit Click here and download it now
August 15th, 2006 6:11pm

You da man!
August 15th, 2006 8:44pm

KirkHaselden wrote:You da man! Did you vote? -J
Free Windows Admin Tool Kit Click here and download it now
August 16th, 2006 12:44am

Hi, I'm not sure if you found out the answer to your question, nor if this is what you are looking for, however, when creating a maintanance plan, you can use a "Maintenance Cleanup Task" to delete files which are older than X days old. The trick to this is to make sure you've ticked the correct boxes. For instance, I setup my backups to backup to G:\SQLBackups and tick the "Create A Subdirectory for each database" option. Therefore, when I put in a corresponding "Maintenance Cleanup Task", I make sure that I enter G:\SQLBackups in the "Folder" box, but also tick the "INCLUDE First Level Subfolders" option. (This caught me out once as I couldnt understand why, even with the task in place, it wasnt deleting...) This definately works for SQL backups, and also SQL transaction logs (just change the file extension to trn, not bak) I'm not sure if you want to delete different files that aren't SQL backups but it MIGHT be possible to simply enter a different file extension in there to delete other types of files... Regards Andy
October 8th, 2010 2:48pm

public void Main() { string directoryPath = @"\\DirectoryPath"; string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.csv"); foreach (string currFile in oldFiles) { FileInfo currFileInfo = new FileInfo(currFile); if (currFileInfo.CreationTime < DateTime.Now.AddDays(-1)) { currFileInfo.Delete(); } } // TODO: Add your code here Dts.TaskResult = (int)ScriptResults.Success; }
Free Windows Admin Tool Kit Click here and download it now
May 24th, 2011 8:56am

Here is an other example with a Script Task: http://microsoft-ssis.blogspot.com/2011/01/use-filedates-in-ssis.html Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com
May 24th, 2011 9:09am

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

Other recent topics Other recent topics