C# Create a Files Remaining Counter

In C# for SSIS, how can I create a create a 'Files Remaining' counter?

I have a function that gets the current number of files in a directory and stores that value in a variable.  I have code that cleans and processes file data in a foreach loop and I would like the file remaining counter to interval down each time it starts a new file.

The function code to get the number of files in the directory is below:

// LOGIC TO COUNT THE NUMBER OF ROWS IN A FILE
        public static long CountLines(string filename)
        {
            long result = 0;
            using (var input = File.OpenText(filename))
            {
                while (input.ReadLine() != null)
                {
                    ++result;
                }
            }
            return result;
        }

Open in new window


and I call or show the variable in the Dts Output by using

            // COUNT THE NUMBER OF FILES IN THE DROPOFF DIRECTORY
            long filecount = Directory.GetFiles(dropfolder).Length;
            Dts.Events.FireInformation(3, "File Count", "Number of files to be processed " + filecount.ToString("#,##0") + " files.", "", 0, ref fireAgain);

Open in new window


Thank you!
Erik HauserBusiness Intelligence DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Do you move the file after you're done processing?  If so you could just repeat the

long filecount = Directory.GetFiles(dropfolder).Length;
            Dts.Events.FireInformation(3, "File Count", "Number of files to be processed " + filecount.ToString("#,##0") + " files.", "", 0, ref fireAgain);

Open in new window


Inside your loop.

If not then you need to create a variable and increment it.  The output would be  filecount - loopcount.

For more information on creating variables:
https://www.mssqltips.com/sqlservertip/3686/configure-the-sql-server-integration-services-for-loop-container/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erik HauserBusiness Intelligence DeveloperAuthor Commented:
Thank you very much. I can move the file afterwards but the increment piece would be the one I need.  Can you please give me an example?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Just re-get the count of files after you move the file.  The number of files in the directory is the number of files to be processed at that point.
0
Erik HauserBusiness Intelligence DeveloperAuthor Commented:
Than you. I was trying not to move the file(s) until all of them are processed but I will take what you've said and figure out how to move files in C#.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
You can do that in SSIS.  Just do a file move in your for each loop.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.