Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check whether directory exists before clean up #452

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/WingetCreateCLI/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ public static class Common
/// <param name="cleanUpDays">The number of days that determine the age of files to be considered for cleanup.</param>
public static void CleanUpFilesOlderThan(string cleanUpDirectory, int cleanUpDays)
{
var logDirectory = new DirectoryInfo(cleanUpDirectory);
var files = logDirectory.GetFiles();
var directory = new DirectoryInfo(cleanUpDirectory);

if (!directory.Exists)
{
return;
}

var files = directory.GetFiles();
foreach (var file in files)
{
if (file.CreationTime < DateTime.Now.AddDays(-cleanUpDays))
Expand All @@ -45,12 +51,12 @@ public static void CleanUpFilesOlderThan(string cleanUpDirectory, int cleanUpDay
}
}

var directories = logDirectory.GetDirectories();
foreach (var directory in directories)
var directories = directory.GetDirectories();
foreach (var subDirectory in directories)
{
if (directory.CreationTime < DateTime.Now.AddDays(-cleanUpDays))
if (subDirectory.CreationTime < DateTime.Now.AddDays(-cleanUpDays))
{
directory.Delete(true);
subDirectory.Delete(true);
}
}
}
Expand Down