一键删除所有的空的文件夹
/***
*
*
* 删除空文件夹
*
*/
using System.Collections.Generic;
using System.IO;
using UnityEditor;
public class RemoveEmptyFolder
{
#region 删除空文件夹
const string title = "Remove Empty Directories";
[MenuItem("ClientPlugins/Remove empty folder")]
public static void CleanEmptyDirectories()
{
var di = new DirectoryInfo("Assets/");
var dis = new List<DirectoryInfo>();
DoRemoveEmptyDirectory(di, dis);
if (dis.Count == 0)
{
EditorUtility.DisplayDialog(title, "No Empty Directory", "OK");
return;
}
var sb = new System.Text.StringBuilder();
for (int i = 0; i < dis.Count; ++i)
{
int index = i + 1;
sb.AppendLine(index.ToString() + " " + dis[i].FullName);
}
foreach (var target in dis)
{
if (File.Exists(target.FullName + ".meta"))
File.Delete(target.FullName + ".meta");
target.Delete(true);
UnityEngine.Debug.Log(target.FullName);
}
AssetDatabase.Refresh();
}
public static bool DoRemoveEmptyDirectory(DirectoryInfo target, List<DirectoryInfo> dis)
{
bool hasDirOrFile = false;
foreach (var di in target.GetDirectories())
{
bool result = DoRemoveEmptyDirectory(di, dis);
if (result) hasDirOrFile = true;
}
foreach (var fi in target.GetFiles())
{
if (!fi.Name.StartsWith(".") && !fi.FullName.EndsWith(".meta"))
{
hasDirOrFile = true;
}
}
if (hasDirOrFile == false)
{
if (dis.Contains(target) == false)
dis.Add(target);
}
return hasDirOrFile;
}
#endregion
}