简体   繁体   English

如何动态重命名文件名

[英]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.我想要这样的文件名:Nr[文件中的第一个数字]-[文件中的最后一个数字].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 100000002 100000010 100000029 100000037 100000045 100000053 100000061 100000088 100000096 1000001

so the file name has to be then: Nr[100000002]-[100000118].txt所以文件名必须是:Nr[100000002]-[100000118].txt

Thank you谢谢

and second file contains the numbers:第二个文件包含数字:

100000126 100000134 100000142 100000150 100000169 100000177 100000185 100000193 100000207 100000215 100000126 100000134 100000142 100000150 100000169 100000177 100000185 100000193 100000207 1000002

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]));
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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