删除特定目录中批量删除部分文件夹名称

时间:2016-08-24 23:16:26

标签: c# .net winforms file rename

在我的“C:\ image \ Test_Directory \”中,我有各种名为{hello1,hello2,hello3,hello4}等文件夹......我想在我的“C:\ image \”下的文件夹中删除“hello”一词Test_Directory \“所以文件夹会读{1,2,3,4}等...我在网上搜索了如何做到这一点,但我没有运气。

示例问题:

enter image description here

目标:

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以使用Directory.Move()重命名文件夹:

foreach (var dirName in Directory.GetDirectories(@"C:\image\Test_Directory"))
{
    string newName = dirName.Replace("Hello", "");
    Directory.Move(dirName, newName);
}

答案 1 :(得分:1)

使用RenameDirectory()函数 试试这个:

 Dim dir As DirectoryInfo = New DirectoryInfo("C:\image\Test_Directory")
    Dim folders As DirectoryInfo() = dir.GetDirectories()
    For Each folder As DirectoryInfo In folders
        My.Computer.FileSystem.RenameDirectory(folder.FullName, folder.Name.Replace("Hello", ""))
    Next