简体   繁体   中英

How to rename dynamically file name

I want to have the file names like this: Nr[first number in file]-[last number in file].txt.

this I have:

   static void Main(string[] args)
        {


            const int linesPerFile = 10;
            string path = @"G:\Folder";
            const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";
            var bans = BankAcoutNumbers.BANS;
            string tempFile;
            //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:

100000002 100000010 100000029 100000037 100000045 100000053 100000061 100000088 100000096 100000118

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

Thank you

and second file contains the numbers:

100000126 100000134 100000142 100000150 100000169 100000177 100000185 100000193 100000207 100000215

Unless your files are very long, you can simply read their full content into the memory, split up their content and rename them.

            // Get files
            DirectoryInfo di = new DirectoryInfo (@"c:\my\path");
            var files = di.GetFiles();

            // Loop through files
            foreach (var file in files) 
            {
                // Read file
                string content = File.ReadAllText (file.FullName);

                // Split text (add as many separators as you want, I'm using
                // whitespace just like in your example)
                string[] numbers = content.Split (' ');

                // First number is numbers[0]
                // Last number is numbers[numbers.Length - 1]

                // Rename file
                File.Move(file.FullName, string.Format("Nr{0}-{1}.txt", numbers[0], numbers[numbers.Length - 1]));
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM