locked
Foreach Loop - Get the Directories RRS feed

  • Question

  • I have several directories I want to loop into. Can I use the Foreach Loop to place the directory path or name into a variable? What enumerator to use?

    TIA

     

    [root] DIR A  +---- DIR01

                        +---- DIR02

        +---- DIR03

    Tuesday, January 18, 2011 5:48 PM

Answers

  • I have several directories I want to loop into. Can I use the Foreach Loop to place the directory path or name into a variable? What enumerator to use?

    TIA

     [root] DIR A  +---- DIR01

                        +---- DIR02

        +---- DIR03

    Here is an example of a Script Task and a standard Foreach Loop Container to loop through the folders:
    http://microsoft-ssis.blogspot.com/2011/01/foreach-folder-enumerator.html

    • Proposed as answer by SSISJoostMVP Thursday, January 20, 2011 9:00 PM
    • Marked as answer by Jerry Nee Wednesday, February 9, 2011 4:54 AM
    Thursday, January 20, 2011 8:49 PM

All replies

  • I think this post describes the technique pretty well: http://consultingblogs.emc.com/jamiethomson/archive/2005/05/30/SSIS_3A00_-Enumerating-files-in-a-Foreach-loop.aspx

    Otherwise please let us know.


    Arthur My Blog
    By: TwitterButtons.com
    Tuesday, January 18, 2011 5:59 PM
  • If I understand your question correctly then I think you want to loopthrough directories not files. If thats the case then try this

    Assume you have package with 2 variables objDirList and varDirPath

    1. Create script task, Enter ReadWriteVariable=objDirList

    Open script and use this code (I have not tested it but sud work) ......... http://msdn.microsoft.com/en-us/library/ms143448.aspx

    Dts.variables("objDirList").Value = IO.Directory.GetDirectories("C:\Projects","*",AllDirectories) '//This will return all folders under C:\Projects.... AllDirectories means Recursive
    


    2. Create ForeachLoop Task
    3. Select Enumrator=For Each Item
    4. Select variable which holds your Directory List (e.g. objDirList)
    5. Go to variable mapping page and map 0 to some variable (e.g. varDirPath)
    6. Now you can access varDirPath in any child task to read full path of directory.

     

    • Edited by Nayan Wednesday, September 2, 2015 8:34 PM
    Tuesday, January 18, 2011 6:32 PM
  • If I understand your question correctly then I think you want to loopthrough directories not files. If thats the case then try this

    Assume you have package with 2 variables objDirList and varDirPath

    1. Create script task, Enter ReadWriteVariable=objDirList

    Open script and use this code (I have not tested it but sud work) ......... http://msdn.microsoft.com/en-us/library/ms143448.aspx

    Dts.variables("objDirList").Value = IO.Directory.GetDirectories("C:\Projects","*",AllDirectories) '//This will return all folders under C:\Projects.... AllDirectories means Recursive
    


    2. Create ForeachLoop Task
    3. Select Enumrator=For Each Item
    4. Select variable which holds your Directory List (e.g. objDirList)
    5. Go to variable mapping page and map 0 to some variable (e.g. varDirPath)
    6. Now you can access varDirPath in any child task to read full path of directory.

     

     


    SSIS-Tutorials-FAQs | Convert DTS to SSIS | Document SSIS | SSIS Tasks | Real-time SSIS Monitoring

    I was hoping that there is a way without a script using the ForEach Loop.  Thanks!

     

     

    Tuesday, January 18, 2011 7:00 PM
  • There is an open source Foreach Directory Enumerator on CodePlex.
    Todd McDermid's Blog Talk to me now on
    Tuesday, January 18, 2011 7:28 PM
  • I have several directories I want to loop into. Can I use the Foreach Loop to place the directory path or name into a variable? What enumerator to use?

    TIA

     [root] DIR A  +---- DIR01

                        +---- DIR02

        +---- DIR03

    Here is an example of a Script Task and a standard Foreach Loop Container to loop through the folders:
    http://microsoft-ssis.blogspot.com/2011/01/foreach-folder-enumerator.html

    • Proposed as answer by SSISJoostMVP Thursday, January 20, 2011 9:00 PM
    • Marked as answer by Jerry Nee Wednesday, February 9, 2011 4:54 AM
    Thursday, January 20, 2011 8:49 PM