Skip to content

Commit cd02b96

Browse files
committed
Feature: Improve check
1 parent e78a716 commit cd02b96

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static void Load()
180180
// Create a backup of the legacy XML file and delete the original
181181
Backup(legacyFilePath,
182182
GetSettingsBackupFolderLocation(),
183-
$"{TimestampHelper.GetTimestamp()}_{GetLegacySettingsFileName()}");
183+
TimestampHelper.GetTimestampFilename(GetLegacySettingsFileName()));
184184

185185
File.Delete(legacyFilePath);
186186

@@ -279,7 +279,7 @@ private static void CreateDailyBackupIfNeeded()
279279
// Create backup
280280
Backup(GetSettingsFilePath(),
281281
GetSettingsBackupFolderLocation(),
282-
$"{TimestampHelper.GetTimestamp()}_{GetSettingsFileName()}");
282+
TimestampHelper.GetTimestampFilename(GetSettingsFileName()));
283283

284284
// Cleanup old backups
285285
CleanupBackups(GetSettingsBackupFolderLocation(),
@@ -302,14 +302,16 @@ private static void CreateDailyBackupIfNeeded()
302302
/// <param name="maxBackupFiles">The maximum number of backup files to retain. Must be greater than zero.</param>
303303
private static void CleanupBackups(string backupFolderPath, string settingsFileName, int maxBackupFiles)
304304
{
305+
// Get all backup files sorted by timestamp (newest first)
305306
var backupFiles = Directory.GetFiles(backupFolderPath)
306-
.Where(f => f.EndsWith(settingsFileName) || f.EndsWith(GetLegacySettingsFileName()))
307-
.OrderByDescending(f => TimestampHelper.ExtractTimestampFromFilename(f))
307+
.Where(f => (f.EndsWith(settingsFileName) || f.EndsWith(GetLegacySettingsFileName())) && TimestampHelper.IsTimestampedFilename(Path.GetFileName(f)))
308+
.OrderByDescending(f => TimestampHelper.ExtractTimestampFromFilename(Path.GetFileName(f)))
308309
.ToList();
309310

310311
if (backupFiles.Count > maxBackupFiles)
311312
Log.Info($"Cleaning up old backup files... Found {backupFiles.Count} backups, keeping the most recent {maxBackupFiles}.");
312313

314+
// Delete oldest backups until the maximum number is reached
313315
while (backupFiles.Count > maxBackupFiles)
314316
{
315317
var fileToDelete = backupFiles.Last();

Source/NETworkManager.Utilities/TimestampHelper.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ public static string GetTimestamp()
1111
return DateTime.Now.ToString("yyyyMMddHHmmss");
1212
}
1313

14+
/// <summary>
15+
/// Generates a filename by prefixing the specified filename with a timestamp string.
16+
/// </summary>
17+
/// <param name="fileName">The original filename to be prefixed with a timestamp. Cannot be null or empty.</param>
18+
/// <returns>A string containing the timestamp followed by an underscore and the original filename.</returns>
19+
public static string GetTimestampFilename(string fileName)
20+
{
21+
return $"{GetTimestamp()}_{fileName}";
22+
}
23+
24+
/// <summary>
25+
/// Determines whether the specified file name begins with a valid timestamp in the format "yyyyMMddHHmmss".
26+
/// </summary>
27+
/// <remarks>This method checks only the first 14 characters of the file name for a valid timestamp and
28+
/// does not validate the remainder of the file name.</remarks>
29+
/// <param name="fileName">The file name to evaluate. The file name is expected to start with a 14-digit timestamp followed by an
30+
/// underscore or other characters.</param>
31+
/// <returns>true if the file name starts with a valid timestamp in the format "yyyyMMddHHmmss"; otherwise, false.</returns>
32+
public static bool IsTimestampedFilename(string fileName)
33+
{
34+
// Ensure the filename is long enough to contain a timestamp
35+
if (fileName.Length < 15)
36+
return false;
37+
38+
var timestampString = fileName.Substring(0, 14);
39+
40+
return DateTime.TryParseExact(timestampString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
41+
}
42+
1443
/// <summary>
1544
/// Extracts the timestamp from a filename that starts with a timestamp prefix.
1645
/// </summary>
@@ -19,31 +48,11 @@ public static string GetTimestamp()
1948
/// If the timestamp cannot be parsed, DateTime.MinValue is returned.</remarks>
2049
/// <param name="filePath">The full path to the file or just the filename.</param>
2150
/// <returns>The timestamp extracted from the filename, or DateTime.MinValue if parsing fails.</returns>
22-
public static DateTime ExtractTimestampFromFilename(string filePath)
51+
public static DateTime ExtractTimestampFromFilename(string fileName)
2352
{
24-
try
25-
{
26-
var fileName = Path.GetFileName(filePath);
27-
28-
// Extract the timestamp prefix (yyyyMMddHHmmss format, 14 characters)
29-
if (fileName.Length >= 14)
30-
{
31-
var timestampString = fileName.Substring(0, 14);
32-
33-
// Parse the timestamp
34-
if (DateTime.TryParseExact(timestampString, "yyyyMMddHHmmss",
35-
CultureInfo.InvariantCulture,
36-
DateTimeStyles.None, out var timestamp))
37-
{
38-
return timestamp;
39-
}
40-
}
41-
}
42-
catch (ArgumentException)
43-
{
44-
// If an argument error occurs, return MinValue to sort this file as oldest
45-
}
46-
47-
return DateTime.MinValue;
53+
// Extract the timestamp prefix (yyyyMMddHHmmss format, 14 characters)
54+
var timestampString = fileName.Substring(0, 14);
55+
56+
return DateTime.ParseExact(timestampString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
4857
}
4958
}

0 commit comments

Comments
 (0)