Welcome to EMC Consulting Blogs Sign in | Join | Help

SSIS Junkie

SSIS: Deleting a group of files

A question I quite often see on the SSIS forum is:

Is there a way to delete a group of files within a directory? ie: I want to delete all files in directorty c:\logs\*.txt

(http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3196521&SiteID=1)

Well its actually quite easy, you just need  ForEach Loop container and the FileSystem Task. Here's how you set up the ForEach Loop container:

In the 'Collection' tab you need to specify the enumerator, the folder from which to delete, and a fie mask to specify the files that you want to delete.

image

In the Variable Mappings tab you need to store the enumerated filename in a pre-created variable. The index will be 0 in this case because we are only interested in the file name which gets returned in the first column of the zero-based enumeration.

image

 

After that we have the simple task of wiring up the File System task that we do like so:

image

Note that we have used the variable "[User::FileName] that we populated using the ForEach Lloop container.

 

And that is pretty much it. Very simple indeed when you know how. I have made a demo package available here:

-Jamie

Published Thursday, April 17, 2008 10:38 PM by jamie.thomson

Comments

 

Joseph said:

Unfortunately, most of my requirements have a time element attached to them (delete files in dir older than x days).  So for these, I've had to drop in to .NET to achieve.  Wish they had these time parameters within the File System task...

April 18, 2008 1:40 PM
 

John said:

The forfiles windows command can be used to delete *.bak files in a folder and subfolders that are xx days old.  I copied the exe from a 2003 server onto my xp desktop in order to get the command line correct before implementing the command as a dos job in a sql server.

April 21, 2008 8:03 PM
 

Terry said:

I've found a Script Task with VB code as follows to work well:

For Each FileFound As String In Directory.GetFiles(CStr(Dts.Variables("vZipExtractPath").Value), "*.sas")

           File.Delete(FileFound)

       Next

       For Each FileFound As String In Directory.GetFiles(CStr(Dts.Variables("vZipExtractPath").Value), "*.xls")

           File.Delete(FileFound)

       Next

       For Each FileFound As String In Directory.GetFiles(CStr(Dts.Variables("vZipExtractPath").Value), "*.exe")

           File.Delete(FileFound)

       Next

       For Each FileFound As String In Directory.GetFiles(CStr(Dts.Variables("vZipExtractPath").Value), "*.zip")

           File.Delete(FileFound)

       Next

The vZipExtractPath variable value is set in my package configuration, but of course could be hard-coded.

April 21, 2008 9:06 PM
New Comments to this post are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems