Recently I needed to get a list of subfolders in SSIS. I also only wanted to bring back a list of subfolders that had the date appended to the end. So let’s walk through how to do this in Script Task using c#.
First we need to create two variables in SSIS:
Next bring a script task into the control flow and open it up for editing.
- Select C# for the script language.
- ReadWriteVariables select objDirectoryList
Now select Edit Script
- Under the section “public void Main()” enter the following code:
- Of course replace the directory location with your directory.
Dts.Variables["objDirectoryList"].Value = System.IO.Directory.GetDirectories(@"C:\Blogs\RootFolder\","*20*",AllDirectories); Dts.TaskResult = (int)ScriptResults.Success; } public SearchOption AllDirectories { get; set; }
Essentially all I’m doing here is populating the object variable objDirectoryList with all the subdirectories in the folder C:\Blogs\RootFolder\. Notice that I have also added a filter here “*20*”. If you do not wish to have a filter and you want to bring back all subdirectories then just replace my filter with “*”.
Finally we can now iterate through this object variable in a FELC.
Below you can see the two directories that are returned from the script task above.
Thanks for looking, hope this helps.
Hi Mitchell, this is very useful article. After you get the list of sub-folders, do you know how to copy those folders to another location?
Please help.
Hey Sara,
You can copy folders using the File System task in SSIS. Of course the list of sub-folders exist inside of an object variable so you need to iterate over the list one by one using the For Each Container.
If you are not familiar with how to iterate over an object variable using the For Each Container you can take a look at this blog/video by Manuel Quintana.
https://sqlrican.wordpress.com/2015/02/23/my-fellow-developers-ask-not-what-you-can-do-for-you-object-variable-ask-what-your-object-variable-can-do-for-you/
Regards,
Mitchell Pearson
Hi Mitchell,
Thank you for your reply. I used object variable and assigned it to for each loop container and used file system task to copy directory using the variable. It is copying the sub folders, but after copying sub folders it is trying to write the files and failing since the file already exists.
Thanks,
Radhika
Never mind, I got it.
Thanks!!