Skip to content

Commit cc0950d

Browse files
CopilotManlyMarco
andauthored
Remove empty directories after mod update file deletion (#250)
When mod updates remove files, their parent directories may be left behind as empty folders. This adds cleanup logic to walk up the directory tree and remove empty directories after each file deletion. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com>
1 parent 1c497cf commit cc0950d

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/KKManager.Updater/Data/UpdateItem.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Security;
45
using System.Threading;
56
using System.Threading.Tasks;
@@ -120,6 +121,23 @@ public async Task Update(Progress<double> progressCallback, CancellationToken ca
120121
{
121122
downloadTarget.MoveTo(TargetPath.FullName);
122123
}
124+
else
125+
{
126+
// File was deleted without replacement, try to remove now-empty parent directories
127+
var gameDir = InstallDirectoryHelper.GameDirectory.FullName;
128+
var dir = TargetPath.Directory;
129+
while (dir != null && dir.Exists && !PathTools.PathsEqual(dir.FullName, gameDir))
130+
{
131+
try
132+
{
133+
if (dir.EnumerateFileSystemInfos().Any()) break;
134+
Console.WriteLine($"Removing empty directory: {dir.FullName}");
135+
dir.Delete(false);
136+
}
137+
catch (Exception ex) { Console.WriteLine($"Failed to remove empty directory {dir.FullName}: {ex.Message}"); break; }
138+
dir = dir.Parent;
139+
}
140+
}
123141
}
124142
}
125143
catch (IOException)

0 commit comments

Comments
 (0)