Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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;
}
}
@Brandon-Gui123
Copy link

Brandon-Gui123 commented Jul 9, 2020

Hello, this is a really helpful script. Thanks for sharing!

I do have some suggestions for this script though:

  1. I don't think it is a good idea to declare a field called subDirectory (on lines 16 and 27) because a subdirectory is a directory below another directory. So far, we're not dealing with a directory inside another directory but rather, just directories themselves. So I suggest it'll be more clearer to call it directory instead. I understand that your usage of GetDirectories also returns subdirectories of the specified directory but when using it in a foreach loop and in ScanDirectory, what we're dealing with is a directory itself.

  2. I suggest changing the LINQ on line 34 to the following because double negatives can be quite confusing (you're "returning true if NOT any element does NOT end with '.meta'", which could be better interpreted as "return true if all elements end with '.meta'"):

filesInSubDirectory.All(t => t.FullName.EndsWith(".meta")
  1. I also suggest deleting the ".meta" file of the folder deleted on line 37. This will help prevent Unity from throwing a warning saying that the ".meta" file for a file exists, but the file is deleted. This is what I suggest to add after subDirectory.Delete(true):
string dirMetaFile = subDirectory.FullName + ".meta";
if (File.Exists(dirMetaFile))
{
    File.Delete(dirMetaFile);
}

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

@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