@@ -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