public IEnumerable <AudioLog> GetFilesOlderThan(DateTime date) { var webRequest = WebRequest.Create($"ftp://{_host}/{_targetDirectory}") as FtpWebRequest; if (webRequest == null) { throw new WebException("Failed to create a web request"); } webRequest.Method = WebRequestMethods.Ftp.ListDirectory; webRequest.Credentials = new NetworkCredential(_username, _password); var webResponse = webRequest.GetResponse() as FtpWebResponse; if (webResponse != null) { using (var stream = webResponse.GetResponseStream()) { if (stream != null) { using (var streamReader = new StreamReader(stream)) { string line; while ((line = streamReader.ReadLine()) != null) { var fullFileName = line.Split('/').Last(); var file = fullFileName.Split('.').First(); DateTime fileTime; if (!DateTime.TryParseExact(file, _format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out fileTime)) { Logger.Warn($"Malformed file name {file}"); continue; } if (fileTime.CompareTo(date) < 0) { yield return(AudioLog.FromPath(line)); } } } } else { throw new WebException("Failed to get the response stream of directory listing"); } } } else { throw new WebException("Failed to get a response of directory listing"); } }
public IEnumerable <AudioLog> GetFilesOlderThan(DateTime date) { if (_destinationDirectory == null) { throw new ArgumentException("Destination directory is not set"); } var dirInfo = new DirectoryInfo(_destinationDirectory); var allFiles = dirInfo.GetFiles("*.mp3", SearchOption.AllDirectories); foreach (var file in allFiles) { DateTime fileTime; if (!DateTime.TryParseExact(file.Name.Split(Char.Parse(".")).First(), _format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out fileTime)) { Logger.Warn($"Malformed file name {file.Name}"); continue; } if (fileTime.CompareTo(date) < 0) { yield return(AudioLog.FromPath(file.Name)); } } }