static List <Tuple <string, DateTime> > GetFilenamesAndDates(string filenamePath, IFilenameDateParser filenameParser) { string _dir = System.IO.Path.GetDirectoryName(filenamePath); string _fnPattern = System.IO.Path.GetFileName(filenamePath); // add in new parser classes for different file formats List <string> _files = System.IO.Directory.GetFiles(_dir, _fnPattern).ToList(); List <Tuple <string, DateTime> > _fileDates = _files.Select(fn => new Tuple <string, DateTime?>(fn, filenameParser.TryParseFilenameDate(fn))) .Where(tpl => tpl.Item2.HasValue) .Select(tpl => new Tuple <string, DateTime>(tpl.Item1, tpl.Item2.Value)) .ToList(); // sort the list of filename/date pairs by date _fileDates.Sort((x, y) => x.Item2.CompareTo(y.Item2)); return(_fileDates); }
/// <summary> /// Parse the dates for all files matching the given wildcard, filter to only include those within the /// requested date range if one has been set on this reader, and store the dates against the /// filenames in a sorted (by date) list of key value pairs date:filename /// </summary> private void PopulateFilenamesAndDates() { string _dir = System.IO.Path.GetDirectoryName(m_FileWildCard); string _fnPattern = System.IO.Path.GetFileName(m_FileWildCard); string[] _files = System.IO.Directory.GetFiles(_dir, _fnPattern); m_FileDates = new SortedList <DateTime, string> (_files .Select(fn => new KeyValuePair <DateTime?, string>(m_FilenameDateParser.TryParseFilenameDate(fn), fn)) .Where(kvp => kvp.Key.HasValue) .Select(kvp => new KeyValuePair <DateTime, string>(kvp.Key.Value, kvp.Value)) .Where(kvp => !m_MinDateToRead.HasValue || kvp.Key >= m_MinDateToRead) .Where(kvp => !m_MaxDateToRead.HasValue || kvp.Key <= m_MaxDateToRead) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) ); }