Rename some files in a folder

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

Example: help.txt to 1help.txt

How can i do this?

+51
c #
Mar 25 '09 at 9:10
source share
6 answers

Take 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)); } } 
+80
Mar 25 '09 at 9:12
source share

The function you are looking for is File.Move(source, destination) of the System.IO namespace. Also consider the DirectoryInfo class (of the same namespace) to access the contents of a folder.

+8
Mar 25 '09 at 9:15
source share

I will just post it here since I needed to write this code for my own purposes.

 using System; using System.IO; public static class FileSystemInfoExtensions { public static void Rename(this FileSystemInfo item, string newName) { if (item == null) { throw new ArgumentNullException("item"); } FileInfo fileInfo = item as FileInfo; if (fileInfo != null) { fileInfo.Rename(newName); return; } DirectoryInfo directoryInfo = item as DirectoryInfo; if (directoryInfo != null) { directoryInfo.Rename(newName); return; } throw new ArgumentException("Item", "Unexpected subclass of FileSystemInfo " + item.GetType()); } public static void Rename(this FileInfo file, string newName) { // Validate arguments. if (file == null) { throw new ArgumentNullException("file"); } else if (newName == null) { throw new ArgumentNullException("newName"); } else if (newName.Length == 0) { throw new ArgumentException("The name is empty.", "newName"); } else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0 || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0) { throw new ArgumentException("The name contains path separators. The file would be moved.", "newName"); } // Rename file. string newPath = Path.Combine(file.DirectoryName, newName); file.MoveTo(newPath); } public static void Rename(this DirectoryInfo directory, string newName) { // Validate arguments. if (directory == null) { throw new ArgumentNullException("directory"); } else if (newName == null) { throw new ArgumentNullException("newName"); } else if (newName.Length == 0) { throw new ArgumentException("The name is empty.", "newName"); } else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0 || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0) { throw new ArgumentException("The name contains path separators. The directory would be moved.", "newName"); } // Rename directory. string newPath = Path.Combine(directory.Parent.FullName, newName); directory.MoveTo(newPath); } } 
+8
Apr 10 '12 at 18:26
source share

Check out How to rename a file in C #? . I did not know that C # has no renaming ... It seems you need to use System.IO.File.Move(oldFileName, newFileName)

+1
Mar 25 '09 at 9:16
source share

You can use File.Move , for example:

 string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName"); string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName"); File.Move(oldFilePath, newFilePath); 
0
Jun 07 '13 at 8:20
source share

In the .NET Framework 4.0, I use the FileInfo.MoveTo() method, which takes only one argument

Just for moving files my method is as follows

 private void Move(string sourceDirName, string destDirName) { DirectoryInfo dir = new DirectoryInfo(sourceDirName); FileInfo[] files = null; files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); file.MoveTo(temppath); } } 

for renaming files my method is as follows

 private void Rename(string folderPath) { int fileCount = 0; DirectoryInfo dir = new DirectoryInfo(folderPath); files = dir.GetFiles(); foreach (FileInfo file in files) { fileCount += 1; string newFileName = fileCount.ToString() + file.Name; string temppath = Path.Combine(folderPath, newFileName); file.MoveTo(temppath); } } 

AS, which you can see to rename the file, its syntax is almost the same as for Move it, you just need to change the filename before using the MoveTo() method.

0
Apr 10 '14 at
source share



All Articles