Skip to content

Commit 34d01ee

Browse files
committed
refactor: 많은 파일도 리스팅 없이 스트리밍으로 바로 삭제
1 parent b25bada commit 34d01ee

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

FocusTimer/App.xaml.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,32 @@ private void DeleteOldLogFilesByPattern(string logDir, string baseFileName, stri
133133
var regex = new Regex(fullPattern);
134134
var searchPattern = $"{baseFileName}*";
135135

136-
var filesToDelete = Directory.GetFiles(logDir, searchPattern)
137-
.Select(filePath => Path.GetFileName(filePath))
138-
.Select(fileName => new { FileName = fileName, Match = regex.Match(fileName) })
139-
.Where(x => x.Match.Success && x.Match.Groups.Count > 1)
140-
.Select(x => new
141-
{
142-
x.FileName,
143-
DateString = x.Match.Groups[1].Value,
144-
FilePath = Path.Combine(logDir, x.FileName)
145-
})
146-
.Where(x => DateTime.TryParseExact(x.DateString, "yyyyMMdd", null,
147-
System.Globalization.DateTimeStyles.None, out var date) && date < cutoffDate)
148-
.ToList();
149-
150-
foreach (var file in filesToDelete)
136+
// EnumerateFiles로 스트리밍 방식 처리 (메모리 효율적)
137+
foreach (var filePath in Directory.EnumerateFiles(logDir, searchPattern))
151138
{
152-
try
139+
var fileName = Path.GetFileName(filePath);
140+
var match = regex.Match(fileName);
141+
142+
// 정규식 매칭 실패 시 스킵
143+
if (!match.Success || match.Groups.Count <= 1)
153144
{
154-
File.Delete(file.FilePath);
145+
continue;
155146
}
156-
catch
147+
148+
var dateString = match.Groups[1].Value;
149+
150+
// 날짜 파싱 및 기한 체크
151+
if (DateTime.TryParseExact(dateString, "yyyyMMdd", null,
152+
System.Globalization.DateTimeStyles.None, out var date) && date < cutoffDate)
157153
{
158-
// 삭제 실패해도 무시 (파일이 사용 중일 수 있음)
154+
try
155+
{
156+
File.Delete(filePath);
157+
}
158+
catch
159+
{
160+
// 삭제 실패해도 무시 (파일이 사용 중일 수 있음)
161+
}
159162
}
160163
}
161164
}

0 commit comments

Comments
 (0)