Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to have the file names like this: Nr[first number in file]-[last number in file].txt.

this I have:

C#
static void Main(string[] args)
        {


            const int linesPerFile = 10;
            string path = @"G:\Folder";
            const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";
            //string fileName = "File";
            var maxNumberOfFiles = 10;
            Stopwatch timer = new Stopwatch();



                var fileCounter = 0;

                if (!Directory.Exists(path))
                {
                   DirectoryInfo di = Directory.CreateDirectory(path);
                }

                var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
                try
                {

                    // foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                    //{
                    var lineCounter = 0;
                    string line;

                    while (fileCounter <= maxNumberOfFiles)
                    {
                        timer.Start();
                        foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                        {
                            if (lineCounter % linesPerFile == 0)
                            {
                                //lineCounter = 0;
                                destiNationFile.Flush();                                
                                destiNationFile.Dispose();
                                destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
                                fileCounter++;
                            }

                            destiNationFile.WriteLine(bank);
                            lineCounter++;
                           
                           
                            
                        }

                        fileCounter++;
                        
                        //}


                    }
                    
                        
                    timer.Stop();
                    // Console.WriteLine(BankAcoutNumbers.BANS.Count());
                    Console.WriteLine(timer.Elapsed.Seconds);
                }
                catch (Exception)
                {

                    throw;
                }
            
                      

            // Keep the console window open in debug mode.

            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        } 


So the first file contains the numbers:

VB
100000002
100000010
100000029
100000037
100000045
100000053
100000061
100000088
100000096
100000118


so the file name has to be then: Nr[100000002]-[100000118].txt

Thank you
Posted
Comments
ZurdoDev 20-Jan-15 9:25am    
Where are you stuck?
[no name] 20-Jan-15 9:29am    
Thank you for your response

I do it now like this:

const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";

But that is not the right way. But how to do it in the foreach. THere I am stuck

1 solution

The problem is that you don't know the name until you have finished processing all teh records.
What I would do is either:
1) Assemble the file data into a List and write it all at once at the end. By this time you know what the file name should be.
Or
2) Write the file to a temporary file, and then use File.Move to change it's name at the end, once I'd closed it.

Once you have all the records processed, working out the file name is trivial.
 
Share this answer
 
Comments
[no name] 20-Jan-15 9:36am    
Can you make a little start. Thank you
OriginalGriff 20-Jan-15 9:58am    
We aren't here to do your work for you!
And this isn't complicated.
What part is giving you difficulty?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900