I have a list of files which their name indicate when they are created. Their name have the convention “yyyyMMdd.txt”. yyyy are years, MM are months and dd are dates. I am trying to write a function that can open only those files which were created within a time range in C#.
For example: I want to access to files which were created between 10/20/2012 to 10/25/2012. Any file that has its name falls in that time interval will be accessed.
The function signature is something similar to this: void dosomething (DateTime from, DateTime to)
Is there any good way to complete this function? Any suggestions would be appreciated.
The filename format you describe is lexical (ordered) so you can use the following approach:
convert the DateTime inputs to the same format as your file names (yyyyMMdd)
read the file names and convert them to integers like 20121026
convert your inputs to integers the same way
now use ordinary greater than >, less than < tests to determine if the date is in the range.