[MenuItem("Tools/Delete Empty Directory")]
public static void RemoveEmptyDirectory()
{
string path = Application.dataPath;
DirectoryInfo dir = new DirectoryInfo(path);
if (dir != null)
{
foreach (var subDir in dir.GetDirectories())
{
DeleteFolderIfEmpty(subDir.FullName);
}
}
}
private static void DeleteFolderIfEmpty(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir != null && dir.Name != "Assets")
{
string ignFile = dir.FullName + "/.DS_Store";
if (File.Exists(ignFile))
{
File.Delete(ignFile);
}
int fileNum = dir.GetFiles().Length;
if ((fileNum == 0) && dir.GetDirectories().Length == 0)
{
string parentPath = dir.Parent.FullName;
string parent = dir.Parent.Name;
Directory.Delete(path);
string meta = path + ".meta";
if (File.Exists(meta))
{
File.Delete(meta);
}
DeleteFolderIfEmpty(parentPath);
}
else
{
foreach (var subDir in dir.GetDirectories())
{
if (subDir.Exists)
{
DeleteFolderIfEmpty(subDir.FullName);
}
}
}
}
}