Click to See Complete Forum and Search --> : How to rename multiple files in a folder automatically


barisserkan
August 29th, 2008, 09:55 AM
Hi every one

I need to rename all files in a folder with one click event in C sharp. Here are details:

- All files have same extension (*.lmd)
- Number of files can change (I need to use a code like *.*)
- All files are in same folder. It can’t change
- I want to rename for shorten file names (to use in a dos based software)
Example: 123456789101112.lmd to 12345.lmd
Or something like
123456789101112.lmd to trial-1.lmd
Ffdsdfsfsfsdfs.lmd to trial-2.lmd
2de24f2234234342423.lmd to trial-3.lmd

Thanks…

foamy
August 29th, 2008, 10:29 AM
using System.IO;



string[] files = Directory.GetFiles("C:\\Test");
int i = 0;
foreach (string file in files)
{
string path = file.Substring(0, file.LastIndexOf('\\'));
File.Move(file, path + "\\newname" + i);
i++;
}


this will rename all files in c:\test to newname1, newname2 and so forth... All you need is to change C:\test to something else and maybe put + ".lmd" on the end...