如何使用一个命令删除名为x_eV的所有子文件夹?
另外,我怎么能这样做,但是我没有删除,而是重命名文件夹?
答案 0 :(得分:0)
这是C#中的解决方案。它可以通过搜索重命名/删除文件夹。确保您的文件夹未在资源管理器中打开,否则可能会发生错误。
using System;
using System.IO;
// ...
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
// RenameFolders(@"D:\Operations", "Work", "New folder");
DeleteFolders(@"D:\Operations", "Ahoy");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Press any key to quit . . . ");
Console.ReadKey(true);
}
static void DeleteFolders(string location, string searchterm, string wildcard = "*")
{
DirectoryInfo[] directories = new DirectoryInfo(location).GetDirectories(wildcard, SearchOption.AllDirectories);
foreach (DirectoryInfo directory in directories)
{
if (directory.Name.IndexOf(searchterm) != -1)
directory.Delete(true);
}
}
static void RenameFolders(string location, string searchterm, string replace, string wildcard = "*")
{
if (searchterm == replace)
return;
DirectoryInfo[] directories = new DirectoryInfo(location).GetDirectories(wildcard, SearchOption.AllDirectories);
for (int i = directories.Length - 1; i >= 0; i--)
{
string replacedName = directories[i].Name.Replace(searchterm, replace);
if (replacedName == directories[i].Name)
continue;
string replacedFullName = Path.Combine(directories[i].Parent.FullName, replacedName);
directories[i].MoveTo(replacedFullName);
}
}