locked
Run the foreach loop container if file exist RRS feed

  • Question

  • Hi All,
    In my SSIS Package I am using For each loop container to load the data from csv file to SQL Table.
    my file will be like abc_120554.csv
    I want to places one script task before the foreach loop container to check if the file exist in the folder which start with abc_.

    what to do for same.
    Regards,
    Vipin Jha

    Thankx & regards, Vipin jha MCP

    Tuesday, August 25, 2015 9:15 AM

Answers

  • Hi Vipin,

    Please check the below link

    http://www.techbrothersit.com/2013/07/ssis-how-to-check-if-file-exists-in.html


    Please Dont forget to mark as answer and Helpful Post. It helps others to find relevant posts to the same question. Milan Das

    • Proposed as answer by Harry Bal Tuesday, August 25, 2015 8:27 PM
    • Marked as answer by Katherine Xiong Friday, September 4, 2015 10:00 AM
    Tuesday, August 25, 2015 11:27 AM
  • Hi Vipin

    You can change it to *Vendor*.csv

    Wednesday, August 26, 2015 4:29 AM

All replies

  • Hi Vipin,

    Please check the below link

    http://www.techbrothersit.com/2013/07/ssis-how-to-check-if-file-exists-in.html


    Please Dont forget to mark as answer and Helpful Post. It helps others to find relevant posts to the same question. Milan Das

    • Proposed as answer by Harry Bal Tuesday, August 25, 2015 8:27 PM
    • Marked as answer by Katherine Xiong Friday, September 4, 2015 10:00 AM
    Tuesday, August 25, 2015 11:27 AM
  • Hi Vipin

    Why do you want to check if the file exists first, if you are using the for each loop then if there are no files that match you criteria [ABC_*.CSV] then the loop would not execute.

    If you do need to do this then you can

    First create a user variable eg 

    fileCount

    Then add add the script component, remember to make the variable a readwritevariable, in the script component you can make a call to the file system to return a list of all the files that match your criteria.

    string[] files = System.IO.Directory.GetFiles(@"c:\", "ABC_*.CSV");

    then depending whet you want to do with this value you can store this list or just the count into a variable.

     Dts.Variables["User::FileCount"].Value = files.Length;

    Remember to make this a readWriteVariable

    Then you can build the script and close it. 

    once you have completed this then drag a constraint from you script task to the forEachLoop, right click on the constraint and select edit, then change the Evaluation operation to Expression and make the expression 

    @[User::fileCount]>0

    now when you run your workflow the foreach loop will only execute when there is a file that will match your criteria.

     

    Tuesday, August 25, 2015 11:27 AM
  • Hi My requirement has been changed now.

    Now I want to load and count only those files which filename contain %vendor%.

    public void Main()
    {

                string TheSource = Dts.Variables["$Package::VendorItem_FilePath"].Value.ToString();

                string[] files = Directory.GetFiles(TheSource, "VENDOR*.csv", System.IO.SearchOption.TopDirectoryOnly);
                Dts.Variables["User::V_Filecount"].Value= files.Length;
          
            }

    what to change in above code .


    Thankx & regards, Vipin jha MCP

    Wednesday, August 26, 2015 3:18 AM
  • Hi Vipin

    You can change it to *Vendor*.csv

    Wednesday, August 26, 2015 4:29 AM