Rename some files in a folder


Question

I have a task of changing the names of some files (that is, adding id to each name dynamically) in a folder using C#.

Example: help.txt to 1help.txt

How can I do this?

1
54
12/21/2013 10:50:26 PM

Have a look at FileInfo.

Do something like this:

void RenameThem()
{
    DirectoryInfo d = new DirectoryInfo("c:/dir/");
    FileInfo[] infos = d.GetFiles("*.myfiles");
    foreach(FileInfo f in infos)
    {
        // Do the renaming here
        File.Move(f.FullName, Path.Combine(f.DirectoryName, "1" + f.Name));
    }
}
86
6/17/2013 2:50:20 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon