Use export date from filename for archive subdirectory

Extract the date from filenames like "2026-01-20.json" instead of
using the current date, so archives are organized by export date
rather than processing date.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 23:20:17 -05:00
parent 6f63f36df0
commit a281f7f1e7

View File

@@ -28,8 +28,8 @@ public class ArchiveService
var jsonDirectory = Path.GetDirectoryName(jsonFilePath)!;
var filesDirectory = GetFilesDirectoryPath(jsonFilePath);
// Create archive subdirectory based on date
var archiveSubdir = DateTime.Now.ToString("yyyy-MM-dd");
// Create archive subdirectory based on export date from filename
var archiveSubdir = GetExportDateFromFilename(jsonFileName) ?? DateTime.Now.ToString("yyyy-MM-dd");
var archivePath = Path.Combine(archiveRoot, archiveSubdir);
Directory.CreateDirectory(archivePath);
@@ -152,6 +152,26 @@ public class ArchiveService
}
}
/// <summary>
/// Extracts the export date from a filename like "2026-01-20.json".
/// Returns null if the filename doesn't match the expected pattern.
/// </summary>
private static string? GetExportDateFromFilename(string filename)
{
var nameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
if (DateTime.TryParseExact(nameWithoutExtension, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out _))
{
return nameWithoutExtension;
}
// Handle filenames with suffix like "2026-01-20_1.json"
var parts = nameWithoutExtension.Split('_');
if (parts.Length > 0 && DateTime.TryParseExact(parts[0], "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out _))
{
return parts[0];
}
return null;
}
/// <summary>
/// Moves a file, falling back to copy+delete for cross-device moves.
/// </summary>