Created
June 27, 2017 17:04
-
-
Save unity3dcollege/1c6ca527d8ae57de735e9c7747084940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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.
@pythonInRelay No problem! Happy to help.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, this is a really helpful script. Thanks for sharing!
I do have some suggestions for this script though:
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 itdirectory
instead. I understand that your usage ofGetDirectories
also returns subdirectories of the specified directory but when using it in aforeach
loop and inScanDirectory
, what we're dealing with is a directory itself.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'"):
subDirectory.Delete(true)
:Please let me know what you think and once again, thank you for sharing this script!