/// <summary> /// Copy podcasts from download location to USB key /// </summary> /// <param name="downloadFolder">folder podcasts are downloaded to</param> /// <param name="destinationFolder">folder podcasts are copied to</param> public void Copy(string downloadFolder, string destinationFolder) { //does the download folder exist? if (!Directory.Exists(downloadFolder)) { throw new FileNotFoundException("Specified download folder does not exist"); } //does the destination folder exist? if (!Directory.Exists(destinationFolder)) { throw new FileNotFoundException("Specified destination folder does not exist"); } //get folders in download folder var folders = Directory.GetDirectories(downloadFolder); var index = 0; //copy files in each folder to destination //if file exists, skip foreach (var folder in folders) { index += 1; OnSubscriptionCopying(index, folders.Length); var podcastName = folder.Substring(folder.LastIndexOf(@"\", StringComparison.Ordinal) + 1); OnPodcastCopying(podcastName); var files = Directory.GetFiles(folder); //reorder files if needed if (Podcasts == null || Podcasts.Count == 0) { Podcasts = GetPodcasts(); } var podcast = (Podcasts.Find(p => p.Name == podcastName)); if (podcast?.Order == Podcast.EpisodeOrder.Chronological) { //order should be reverse of how they were downloaded //Array.Reverse(files); //Array.Reverse reorders the array but USB clients (home receiver and car) seem to be listing in alpha (despite car docs indicating write order) } //count files as they are processed var fileIndex = 0; foreach (var file in files) { //get source path var filename = Path.GetFileName(file) ?? "IDK"; //get destination path var podcastFolder = Path.Combine(destinationFolder, podcastName); //destination filename is used by player to organize //default filename is number prefix containing download order //if want downloaded last (first podcast) to be first, need to reverse order here var destination = filename; if (podcast?.Order == Podcast.EpisodeOrder.Chronological) { //reset destination to the reverse number order, same prefix destination = (files.Length - fileIndex).ToString("000") + "_" + filename.Substring(4); } //if the destination has leading zero, trim it if (files.Length < 100) { destination = destination.Substring(1); } //replace underscore with space destination = destination.Replace('_', ' '); //append path to destination destination = Path.Combine(podcastFolder, destination); try { if (!File.Exists(destination)) { VerifyFolderExists(podcastFolder); OnEpisodeCopying(podcastName, file, destination); File.Copy(file, destination, false); OnEpisodeCopied(podcastName, file, destination); } } catch (Exception) { OnEpisodeCopyFailed(filename, destination); #if (DEBUG) { throw; } #endif } finally { fileIndex += 1; } } OnPodcastCopied(podcastName); } OnSubscriptionCopied(); }