public static void renameDir(DirectoryInfo dir, string userADD) { System.DateTime dt = System.DateTime.Now; string timeHolder = String.Format("{0:MMddyyyy}", dt); DirectoryInfo renamed = new DirectoryInfo(dir.FullName + "\\renamed\\"); if (!renamed.Exists) renamed.Create(); foreach (FileInfo file in dir.GetFiles()) { string temp = (renamed.FullName + "\\" + file.Name.Substring(0, file.Name.Length - 4) + "_" + userADD + "_" + timeHolder + file.Extension); //file.CopyTo(temp); File.Move(file.FullName, temp); } foreach (DirectoryInfo subDir in dir.GetDirectories()) { if (!subDir.Name.Equals("renamed")) renameDir(subDir , userADD); } }
Monday, July 12, 2010
Renaming All Files in a Directory - C#
The following method searches a given directory (dir), and all subdirectories for files. Each file is renamed with a time stamp and an additional tag specified by a user (userADD).
Labels:
C#,
C# Sample Code
0 comments:
Post a Comment