hi ihave many files like
File.txt
anotherfile.txt
Log.txt . Etc...
And i want to rename these files into something like this
File1.txt
File2.txt
File3.txt . Etc
So how can i do this in c#
Thank you for your help

Recommended Answers

All 2 Replies

Are these files in one folder? If so, you can get all files from this folder into a FileInfo[] (you need fileInfo, because you need a full path, so set files back where they were before renaiming).
Then you do some additional code of renaiming and use Move method for an actual renaiming:

string path =@"C:\MyFolder";
            FileInfo[] files = new DirectoryInfo(path).GetFiles("*.txt");
            for (int i = 0; i < files.Length; i++)
            {
                string newFile = String.Format("{0}{1}.{2}", "File", (i + 1), "txt");
                string fullNewPath = files[i].DirectoryName;
                File.Move(files[i].FullName, Path.Combine(fullNewPath, newFile));
            }

I hope you like it.

Thanks and i liked it so much and it worked perfectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.