重命名文件夹c#中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12347881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Renaming files in folder c#
提问by Jason Paddle
I have over 1000 files in a folder with names like abc_1, abc_2 ... abc_n
我在一个文件夹中有 1000 多个文件,名称类似于 abc_1、abc_2 ... abc_n
I want to delete this prefix 'abc_' from all the files. Any chance to not doing this manually because there are over 1000, and it will be a pain.
我想从所有文件中删除这个前缀“abc_”。任何不手动执行此操作的机会,因为有 1000 多个,这将是一种痛苦。
How can do this with c# ?
如何用 c# 做到这一点?
采纳答案by Aghilas Yakoub
You can try with this code
您可以尝试使用此代码
DirectoryInfo d = new DirectoryInfo(@"C:\DirectoryToAccess");
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
File.Move(f.FullName, f.FullName.Replace("abc_",""));
}
回答by Freeman
you can use a foreach iteration along with the File class from the System.IO namespace.
您可以将 foreach 迭代与 System.IO 命名空间中的 File 类一起使用。
All its methods are provided for you at no cost here: http://msdn.microsoft.com/en-us/library/system.io.file%28v=vs.100%29.aspx
这里免费为您提供所有方法:http: //msdn.microsoft.com/en-us/library/system.io.file%28v=vs.100%29.aspx
回答by Hassan Gulzar
You can enumerate the file.
您可以枚举文件。
using System.IO;
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
Then, ForEach the string[] and create a new instance of the IO.Fileobject.
然后,ForEach 字符串[] 并创建IO.File对象的新实例。
Once you get a handle on a File, just call the Move method and pass in String.Replace("abc_", String.Empty).
一旦获得文件的句柄,只需调用 Move 方法并传入 String.Replace("abc_", String.Empty)。
I said Move because there is no direct Rename method in IO.File.
我说Move是因为IO.File中没有直接的Rename方法。
File.Move(oldFileName, newFileName);
File.Move(oldFileName, newFileName);
Be mindful of the extension.
请注意扩展名。
回答by Oliver
Total Commanderhas the possibility to rename multiple files(You don't need to program a tool on your own for each little task).
Total Commander可以重命名多个文件(您不需要为每个小任务自己编写一个工具)。
回答by Tim Schmelter
You can use File.Moveand String.Substring(index):
您可以使用File.Move和String.Substring(index):
var prefix = "abc_";
var rootDir = @"C:\Temp";
var fileNames = Directory.EnumerateFiles(rootDir, prefix + "*", SearchOption.AllDirectories);
foreach(String path in fileNames)
{
var dir = Path.GetDirectoryName(path);
var fileName = Path.GetFileName(path);
var newPath = Path.Combine(dir, fileName.Substring(prefix.Length));
File.Move(path, newPath);
}
Note: Directory.EnumerateFiles(rootDir, prefix + "*", SearchOption.AllDirectories);will search also subfolders from your root directory. If this is not intended use SearchOption.TopDirectoryOnly.
注意:Directory.EnumerateFiles(rootDir, prefix + "*", SearchOption.AllDirectories);还将搜索根目录中的子文件夹。如果这不是预期用途SearchOption.TopDirectoryOnly。
回答by Mark
You should have a look at the DirectoryInfoclass and GetFiles() Method. And have a look at the Fileclass which provides the Move() Method.
您应该查看DirectoryInfo类和 GetFiles() 方法。并查看提供 Move() 方法的File类。
File.Move(oldFileName, newFileName);
回答by Vignesh.N
string path = @"C:\NewFolder\";
string[] filesInDirectpry = Directory.GetFiles(path, "abc*");
forearch(string file in filesInDirectory)
{
FileInfo fileInfo = new FileInfo(file);
fileInfo.MoveTo(path + "NewUniqueFileNamHere");
}
回答by crypted
Following code will work, not tested though,
以下代码将起作用,但未经测试,
public class FileNameFixer
{
public FileNameFixer()
{
StringToRemove = "_";
StringReplacement = "";
}
public void FixAll(string directory)
{
IEnumerable<string> files = Directory.EnumerateFiles(directory);
foreach (string file in files)
{
try
{
FileInfo info = new FileInfo(file);
if (!info.IsReadOnly && !info.Attributes.HasFlag(FileAttributes.System))
{
string destFileName = GetNewFile(file);
info.MoveTo(destFileName);
}
}
catch (Exception ex)
{
Debug.Write(ex.Message);
}
}
}
private string GetNewFile(string file)
{
string nameWithoutExtension = Path.GetFileNameWithoutExtension(file);
if (nameWithoutExtension != null && nameWithoutExtension.Length > 1)
{
return Path.Combine(Path.GetDirectoryName(file),
file.Replace(StringToRemove, StringReplacement) + Path.GetExtension(file));
}
return file;
}
public string StringToRemove { get; set; }
public string StringReplacement { get; set; }
}
you can use this class as,
你可以使用这个类,
FileNameFixer fixer=new FileNameFixer();
fixer.StringReplacement = String.Empty;
fixer.StringToRemove = "@@";
fixer.FixAll("C:\temp");
回答by Lloyd
回答by Brent
I like the simplicity of the answer with the most up-votes, but I didn't want the file path to get modified so I changed the code slightly ...
我喜欢答案的简单性和最多的投票,但我不想修改文件路径,所以我稍微更改了代码......
string searchString = "_abc_";
string replaceString = "_123_";
string searchDirectory = @"\unc\path\with\slashes\";
int counter = 0;
DirectoryInfo d = new DirectoryInfo(searchDirectory);
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
if (f.Name.Contains(searchString))
{
File.Move(searchDirectory+f.Name, searchDirectory+ f.Name.Replace(searchString, replaceString));
counter++;
}
}
Debug.Print("Files renamed" + counter);

