Skip to content

Instantly share code, notes, and snippets.

@ericdrobinson
Created April 10, 2017 19:03
Show Gist options
  • Save ericdrobinson/1fc4cd1afef228d904f85e4e91e68baf to your computer and use it in GitHub Desktop.
Save ericdrobinson/1fc4cd1afef228d904f85e4e91e68baf to your computer and use it in GitHub Desktop.
Prune Empty Folders in Unity Projects
using UnityEngine;
using UnityEditor;
using System.IO;
[InitializeOnLoad]
public class ProjectCleaner
{
static int FilesDeleted = 0;
static int DirsDeleted = 0;
static ProjectCleaner()
{
FindAndDeleteEmptyDirectories(Application.dataPath);
if (DirsDeleted > 0 || FilesDeleted > 0)
{
Debug.Log("<i>Project Cleaner</i> script deleted: <b>" + DirsDeleted + "</b> empty folder(s) and <b>" + FilesDeleted + "</b> related meta files.");
}
AssetDatabase.Refresh();
}
static void FindAndDeleteEmptyDirectories(string dirPath)
{
string[] dirs = Directory.GetDirectories(dirPath);
foreach (string dir in dirs)
{
// First clear out any potential directories found internally.
FindAndDeleteEmptyDirectories(dir);
// Then check if the folder is empty.
if (Directory.GetFileSystemEntries(dir).Length == 0)
{
Debug.Log("Deleting empty directory: \"<color=red>" + dir + "</color>\"");
string metaPath = dir + ".meta";
// Check for a meta file sitting next to the directory.
if (File.Exists(metaPath))
{
Debug.Log("Deleting empty directory meta file: \"<color=red>" + metaPath + "</color>\" ");
// Delete the empty folder's meta file.
File.Delete(metaPath);
FilesDeleted++;
}
// Delete the empty folder itself.
Directory.Delete(dir);
DirsDeleted++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment