Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use SSIS Foreach loop to loop multiple folders

I have one main folder, which contains 40+ folders. I have created an SSIS package that creates subfolders in 22 of the 40 folders. I want to use a Foreach Loop in my package to loop through only the 22 folders for my script to return the names and date/timestamp of new subfolders created.

I can currently loop through all 40 folders, but I have not been able to locate information on how to target specific folders.

Any suggestions on references are appreciated.

like image 846
David F Avatar asked Apr 26 '26 11:04

David F


1 Answers

You can generate a list of specific folder using a Script Task, or you can use the for each loop with an expression task to achieve this, just follow my answers on:

  • WildCards in SSIS Collection {not include} name xlsx
  • Exclude specific Sub Folders

Script Example:

Public Sub Main()
    Dim lstFiles As New Generic.List(Of String)

    'Assuming that C:\Temp is the main folder
    'And We want to include all subdirectories that contains "Report" word

    For Each strDirectory As String In IO.Directory.GetDirectories("C:\Temp", "*.*", IO.SearchOption.TopDirectoryOnly)

        If Not strDirectory.Contains("Report") Then Continue For

        lstFiles.AddRange(IO.Directory.GetFiles(strDirectory, "*.*", IO.SearchOption.TopDirectoryOnly)

Next


    Dts.Variables.Item("FilesList").Value = lstFiles

    Dts.TaskResult = ScriptResults.Success
End Sub
like image 184
Hadi Avatar answered Apr 28 '26 01:04

Hadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!