Wednesday, 14 December 2011

Delete files from specified folder using Script Task in SQL Server - SSIS by serverku

Earlier we have seen for the file deletion using File System Task in SSIS. We have used it in Foreach Loop Container. Used a variable to hold file names which are passed from earlier stage one by one and then finally used by the File System Task to delete it.

Now I am going to use the Script Task to delete all files from a specified folder. Here I have added script to get each file from specified folder and then delete them as you can see in the following steps. Let's start to follow them.

1. Drag and drop Script Task.


2. Open script editor from the properties.


3. Apply attached script in editor and save it.


Please note here we need to import system.IO namespace.

4. Turn on the final step and run package. Files get deleted.


You can also find script code here,
--// You need to apply below one line in "namespaces" region.
using System.IO;

--//You need to apply below lines inplace of  // TODO: Add your code here

 string directoryPath = @"E:\TestFolder";
            string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.txt");
            foreach (string currFile in oldFiles)
            {
                FileInfo currFileInfo = new FileInfo(currFile);
                currFileInfo.Delete();
             
            }
Hope you liked this post. Stay tuned for more.

No comments:

Post a Comment

Please Use Good Leanguage