0

rename all files in a dir

Free lines C# 1 revisions 124 5 months ago 5 months ago
void Main()
{
    try {
		DirectoryInfo d = new DirectoryInfo(@"Z:\test\");
		char delim = '.';

		foreach (var file in d.GetFiles())
		{
			String[] fileNameSplits = file.FullName.Split(delim);
    		int extensionIndex = fileNameSplits.Length - 1;
			Directory.Move(file.FullName, @"Z:\test\" + rand(32) + "." + fileNameSplits[extensionIndex]);
		}
	} catch (Exception ex) {
		Console.WriteLine(ex.Message);
    }
}

private static Random random = new Random();
public static string rand(int length)
{
    const string chars = "abcdefghijklmnopqrstuvwxyz"
					   + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
					   + "0123456789";
    return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
}