重命名文件夹中的文件夹 [英] Renaming folders in a folder
问题描述
你好,
我需要知道如何从文件夹内的文件夹名称中删除字符.
因此,例如:如果文件夹名称为MACED_Jul_2010.
然后我要删除"MACED"之后的所有内容.
有帮助吗?
请使用控制台应用程序进行C#编程.
预先谢谢您.
Hi there,
I need to know how to remove characters from folder names inside a folder.
So for example : If the folder name is MACED_Jul_2010.
Then I want to remove everything after the "MACED".
Any help?
Please, in C# programming with a console application.
Thank you in advance.
推荐答案
嗨
我现在懒得用C#编写控制台应用程序,但这是我不时使用的功能强大的小型免费软件工具.
高级重命名器: http://aren.hulubulu.net/ [
Hi
I''m to lazy to write a console app in c# now but here is a powerful little freeware tool that I use from time to time.
Advanced Renamer: http://aren.hulubulu.net/[^]
Hope you come right ;)
它将递归地重命名所有文件夹,文件夹中的文件夹中的文件夹.
It will recursively rename all the folders, folders in folders in folders.
static int _index = 0;
public void RenameFolder(string sourceFolder)
{
try
{
string[] folders = Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = Path.GetFileName(folder);
RenameFolder(folder);
if (name.ToLower().Contains("maced"))
{
string path = Path.GetDirectoryName(folder);
Directory.Move(folder, path + @"\"+ name.Substring(0,5)+ _index.ToString());
_index++;
}
}
}
catch (Exception p1)
{
MessageBox.Show("Directory is invalid!! " + p1.Message);
}
}
如果有帮助,请投票并接受答案.
Please vote and Accept Answer if it Helped.
要重命名文件,您实际上使用System.IO的File.Move方法
要查找名为Maced ******的文件,您需要遍历文件/目录,并检查名称==您想要查找的名称.
例如,
To rename a file you actually use the File.Move method of System.IO
to find the files named Maced****** you''l need to loop through your files/directories and check if the name == the name you wanna find.
for example,
Using System.IO;
System.IO.File.Move(oldFileName, newFileName); //Renames Files
//Putting All files into an array using the directory''s path.
string[] array1 = Directory.GetFiles(@"C:\Maced_Jul_2010");
For(int i = 0; i < array1.length; i++)
{
if(array1[i].contains("Maced") {
System.IO.File.Move(array1[i], indexof(0, 5)); //Renames Files
}
}
注意:
我只是注意到这将不起作用-您在那里的每个文件都将具有相同的文件名-并且最终将失败..我认为您的意思是从其中删除"MACED"一词文件名?在这种情况下,除了if语句内部,其他代码相同-
Note:
i just noticed that this will not work - you''re going to have the same file name for every file in there - and that will end up falling over.. i think you meant to have the word ''MACED'' removed from the filename? in which case, same code except inside the if statement -
System.IO.File.Move(array1[i], indexof(5, array1[i].length - 5));
这篇关于重命名文件夹中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!