Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save unity3dcollege/1c6ca527d8ae57de735e9c7747084940 to your computer and use it in GitHub Desktop.
Save unity3dcollege/1c6ca527d8ae57de735e9c7747084940 to your computer and use it in GitHub Desktop.
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class CleanEmptyFoldersEditorExtension : EditorWindow
{
private static string deletedFolders;
[MenuItem("Tools/Clean Empty Folders")]
private static void Cleanup()
{
deletedFolders = string.Empty;
var directoryInfo = new DirectoryInfo(Application.dataPath);
foreach(var subDirectory in directoryInfo.GetDirectories("*.*", SearchOption.AllDirectories))
{
if (subDirectory.Exists)
{
ScanDirectory(subDirectory);
}
}
Debug.Log("Deleted Folders:\n" + (deletedFolders.Length > 0 ? deletedFolders : "NONE"));
}
private static string ScanDirectory(DirectoryInfo subDirectory)
{
Debug.Log("Scanning Directory: " + subDirectory.FullName);
var filesInSubDirectory = subDirectory.GetFiles("*.*", SearchOption.AllDirectories);
if (filesInSubDirectory.Length == 0 ||
!filesInSubDirectory.Any(t => t.FullName.EndsWith(".meta") == false))
{
deletedFolders += subDirectory.FullName + "\n";
subDirectory.Delete(true);
}
return deletedFolders;
}
}
@825i
Copy link

825i commented Oct 8, 2020

@Brandon-Gui123

Please let me know what you think and once again, thank you for sharing this script!

Hey there! I forked this script and added the changes you mentioned.

Here's the link to my own script. Could you please give me some feedback on this? I changed some other things too, to better follow style guidelines and so on. Thanks for providing your own feedback even 3 years after this script was made.

@Brandon-Gui123
Copy link

@pythonInRelay No problem! Happy to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment