/// <summary> /// /// </summary> /// <param name="sourceFile"></param> /// <param name="destinationPathRoot"></param> /// <returns></returns> private DestinationPaths BuildDestinationPaths(FileInfo sourceFile, string destinationPathRoot) { int year = sourceFile.CreationTime.Year; int month = sourceFile.CreationTime.Month; string monthName = GetMonthName(month); int day = sourceFile.CreationTime.Day; string filename = sourceFile.Name; string numericDate = String.Format("{0}{1}{2}", year.ToString("D4"), month.ToString("D2"), day.ToString("D2")); var output = new DestinationPaths(); // Example Path: G:\Images\2016\May\20160528\Originals\file.jpg string finalFolderPath = Properties.Settings.Default.FinalDestinationFolderName; output.OriginalsPath = String.Format(@"{0}\{1}\{2}\{3}\{4}\{5}", destinationPathRoot, year, monthName, numericDate, finalFolderPath, filename); output.EditsPath = String.Format(@"{0}\{1}\{2}\{3}\{4}", destinationPathRoot, year, monthName, numericDate, "Edits"); output.FinalPath = String.Format(@"{0}\{1}\{2}\{3}\{4}", destinationPathRoot, year, monthName, numericDate, "Final"); return(output); }
private async void buttonStart_Click(object sender, RoutedEventArgs e) { buttonStart.IsEnabled = false; int currentFileCount = 0; try { ShowProgressBar(true); IEnumerable <string> fileList = Directory.EnumerateFiles(textBoxSourceFolder.Text); int fileCount = fileList.Count <string>(); progressBar.Maximum = fileCount; foreach (string filename in fileList) { using (FileStream SourceStream = File.Open(filename, FileMode.Open)) { // Check if file already exists in destination folder string destinationPathRoot = textBoxDestinationFolder.Text; FileInfo fi = new FileInfo(filename); DestinationPaths paths = BuildDestinationPaths(fi, destinationPathRoot); // This the folder where the files will be copied. string destinationFolderAndFile = paths.OriginalsPath; string finalPathOnly = destinationFolderAndFile.Substring(destinationFolderAndFile.LastIndexOf('\\')); if (!File.Exists(destinationFolderAndFile)) { // Create the target directory if it doesn't exist already FileInfo fi2 = new FileInfo(destinationFolderAndFile); DirectoryInfo di = Directory.CreateDirectory(fi2.DirectoryName); // Create the other directories (No files will be copied into them) if (_createEditsFolder) { di = Directory.CreateDirectory(paths.EditsPath); } if (_createFinalFolder) { di = Directory.CreateDirectory(paths.FinalPath); } using (FileStream DestinationStream = File.Create(destinationFolderAndFile)) { UpdateStatus(String.Format("Copying '{0}'", filename)); await SourceStream.CopyToAsync(DestinationStream); progressBar.Value = ++currentFileCount; string status = string.Format("'{0}' copied successfully.", filename); UpdateStatus(status); } } else { string text = string.Format("'{0}' already exists. No work done.", destinationFolderAndFile); UpdateStatus(text); } } if (_removeSourceFileAfterCopy) { File.Delete(filename); UpdateStatus(string.Format("'{0}' removed from source.", filename)); } } ShowProgressBar(false); UpdateStatus(String.Format("{0} files {1}.", currentFileCount, _removeSourceFileAfterCopy ? "moved" : "copied")); UpdateStatus("File Transfer Complete."); } catch (Exception ex) { UpdateStatus(ex.Message.ToString()); } finally { buttonStart.IsEnabled = true; } }