Run the foreach loop container if file exist
August 25th, 2015 5:18am

Hi Vipin,

Please check the below link

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

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 7:30am

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.

 

August 25th, 2015 7:31am

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 .

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 11:21pm

Hi Vipin

You can change it to *Vendor*.csv

August 26th, 2015 12:33am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
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