Skip to content

Commit

Permalink
Adjustment for handling Placeholders for File and sFTP Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
RNoeldner committed Mar 24, 2023
1 parent bae0b82 commit e1608e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
36 changes: 24 additions & 12 deletions Library/ClassLibraryCSV/FileSystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,22 @@ public static string GetAbsolutePath(this string? fileName, string? basePath)
}
}


public static string? GetLatestFileOfPattern(string folder, in string searchPattern)
/// <summary>
/// Get teh last file of a given pattern in a folder
/// </summary>
/// <param name="folder">The dierectory to look in</param>
/// <param name="searchPattern">The pattern to look for</param>
/// <returns>No matching file is found of teh folder does not exists an empty string is returned</returns>
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,
Expand All @@ -260,7 +265,7 @@ public static string GetAbsolutePath(this string? fileName, string? basePath)
lastFile = fileName;
}

return lastFile?.RemovePrefix();
return lastFile.RemovePrefix();
}

/// <summary>
Expand Down Expand Up @@ -558,23 +563,30 @@ public static string RemovePrefix(this string path)
: path;
}

public static string? ResolvePattern(string? fileName)
/// <summary>
/// Resolve placeholders in file names and find the latest fiel to match the pattern
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
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)
Expand Down
14 changes: 4 additions & 10 deletions Library/ClassLibraryCSV/FileWriter/BaseFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,11 @@ public abstract class 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<Column>() : new List<Column>(columnDefinition);
FileSettingDisplay = fileSettingDisplay;
Expand Down

0 comments on commit e1608e4

Please sign in to comment.