diff --git a/Library/ClassLibraryCSV/FileSystemUtils.cs b/Library/ClassLibraryCSV/FileSystemUtils.cs index 426a189a..59e80e2a 100644 --- a/Library/ClassLibraryCSV/FileSystemUtils.cs +++ b/Library/ClassLibraryCSV/FileSystemUtils.cs @@ -237,17 +237,22 @@ public static string GetAbsolutePath(this string? fileName, string? basePath) } } - - public static string? GetLatestFileOfPattern(string folder, in string searchPattern) + /// + /// Get teh last file of a given pattern in a folder + /// + /// The dierectory to look in + /// The pattern to look for + /// No matching file is found of teh folder does not exists an empty string is returned + public static string GetLatestFileOfPattern(string folder, in string searchPattern) { if (string.IsNullOrEmpty(folder)) folder = "."; else if (!DirectoryExists(folder)) - return null; + return string.Empty; // If a pattern is present in the folder this is not going to work var newSet = new DateTime(0); - string? lastFile = null; + var lastFile = string.Empty; foreach (var fileName in Directory.EnumerateFiles( folder.LongPathPrefix(), searchPattern, @@ -260,7 +265,7 @@ public static string GetAbsolutePath(this string? fileName, string? basePath) lastFile = fileName; } - return lastFile?.RemovePrefix(); + return lastFile.RemovePrefix(); } /// @@ -558,23 +563,30 @@ public static string RemovePrefix(this string path) : path; } - public static string? ResolvePattern(string? fileName) + /// + /// Resolve placeholders in file names and find the latest fiel to match the pattern + /// + /// + /// + public static string ResolvePattern(in string fileName) { if (fileName == null || fileName.Length == 0) return string.Empty; - // Handle date Placeholders - fileName = fileName.PlaceholderReplaceFormat("date", DateTime.Now.ToString(CultureInfo.CurrentCulture)) + var withoutPlaceHolder = fileName.PlaceholderReplaceFormat("date", DateTime.Now.ToString(CultureInfo.CurrentCulture)) .PlaceholderReplaceFormat("utc", DateTime.UtcNow.ToString(CultureInfo.CurrentCulture)) .PlaceholderReplace("CDate", string.Format(new CultureInfo("en-US"), "{0:dd-MMM-yyyy}", DateTime.Now)) - .PlaceholderReplace("CDateLong", string.Format(new CultureInfo("en-US"), "{0:MMMM dd\\, yyyy}", DateTime.Now)) ?? string.Empty; + .PlaceholderReplace("CDateLong", string.Format(new CultureInfo("en-US"), "{0:MMMM dd\\, yyyy}", DateTime.Now)); // only if we have wildcards carry on if (fileName.IndexOfAny(new[] { '*', '?', '[', ']' }) == -1) - return fileName; + return withoutPlaceHolder; + + // Handle Placeholders + var split = SplitPath(withoutPlaceHolder); - var split = SplitPath(fileName); - return GetLatestFileOfPattern(split.DirectoryName, split.FileName); + // search for the file + return GetLatestFileOfPattern(split.DirectoryName, split.FileName)!; } public static string GetFileName(in string? path) diff --git a/Library/ClassLibraryCSV/FileWriter/BaseFileWriter.cs b/Library/ClassLibraryCSV/FileWriter/BaseFileWriter.cs index 31c62a3e..1f8df936 100644 --- a/Library/ClassLibraryCSV/FileWriter/BaseFileWriter.cs +++ b/Library/ClassLibraryCSV/FileWriter/BaseFileWriter.cs @@ -81,17 +81,11 @@ protected BaseFileWriter( SourceTimeZone = sourceTimeZone; TimeZoneAdjust = timeZoneAdjust; m_PublicKey = publicKey; - FullPath = FileSystemUtils.ResolvePattern(fullPath) ?? string.Empty; + FullPath = fullPath; var fileName = FileSystemUtils.GetFileName(FullPath); - Header = ReplacePlaceHolder( - header, - fileName, - id); - - m_Footer = ReplacePlaceHolder( - footer, - fileName, - id); + Header = ReplacePlaceHolder(header, fileName, id); + m_Footer = ReplacePlaceHolder(footer, fileName, id); + ValueFormatGeneral = valueFormatGeneral ?? ValueFormat.Empty; ColumnDefinition = columnDefinition == null ? new List() : new List(columnDefinition); FileSettingDisplay = fileSettingDisplay;