• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 66
  • Last Modified:

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!
0
Erik Hauser
Asked:
Erik Hauser
  • 3
  • 2
1 Solution
 
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
 
Erik HauserSenior Technical ConsultantAuthor 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 HauserSenior Technical ConsultantAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now