/// <summary> /// Button Start Sorting /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { // ----- Start ----- if ((string)btnStart.Tag != "run") { // ----- Check if path exists ----- if (!System.IO.Directory.Exists(txtDestFolder.Text)) { MessageBox.Show(lng("DestDirectoryNotExits", "Destination directory not exits!"), lng("Error", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (olvFolders.Items.Count == 0) { MessageBox.Show(lng("W_NoInputFolders", "No input folders!"), lng("Error", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // ----- Thread Settings ----- sortSettings set; set.ClearFolder = chbClearDestFolder.Checked; set.SetImgTime = chbSetImgDate.Checked; set.WriteShiftedTime = chbWriteToExif.Checked; set.DestFolder = txtDestFolder.Text; set.FileMask = cbFileMask.Text; // ----- Create thread ----- process = new AbortableBackgroundWorker(); process.DoWork += Sorting; process.RunWorkerCompleted += WorkComplete; process.ProgressChanged += ProgressChanged; process.RunWorkerAsync(set); // start thread process.WorkerSupportsCancellation = true; process.WorkerReportsProgress = true; // turn on reporting // ----- Set button settings ----- btnStart.Text = lng("Stop", "Stop"); btnStart.Tag = "run"; txtLog.Text = ""; progBar.Value = 0; } // ----- Stop ----- else { // ----- Cancel Thread ----- process.CancelAsync(); process.Abort(); process.Dispose(); } }
/// <summary> /// Sorting function /// </summary> /// <param name="sender">Worker</param> /// <param name="e"></param> private void Sorting(object sender, DoWorkEventArgs e) { const int progSearch = 50; DateTime date; bool rewriteExif; PhotoList = new List <PhotoFile>(); sortSettings set = (sortSettings)e.Argument; AbortableBackgroundWorker worker = (AbortableBackgroundWorker)sender; float progress = 0; float progresFInc = 0, progresInc = 0; // ----- Check input folders ----- int folderCount = olvFolders.Items.Count; if (folderCount == 0) { Log(lng("Warning", "Warning") + ": " + lng("W_NoInputFolders", "No input folders!"), Color.Orange); return; } progresFInc = progSearch / folderCount; // Compute progressBar Increment // ----- Creating file list ----- Log(lng("CreatingFileList", "Creating file list") + "...", Color.Black); foreach (ffolder item in olvFolders.Objects) { string procFile = ""; try { // ----- Search files in folders ----- string[] fileList = System.IO.Directory.GetFiles(item.Path); Log(lng("SearchIn", "Search in:") + " " + item.Path + " (" + fileList.Length.ToString() + ")", Color.Black); progresInc = progresFInc / fileList.Length; // Compute progressBar Increment foreach (string file in fileList) { procFile = System.IO.Path.GetFileName(file); // get file string ext = System.IO.Path.GetExtension(file).ToLower(); // check Extension if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".tiff") { rewriteExif = false; dateImgStat status; date = GetImgDate(file, out status); // read file date if (status == dateImgStat.File) { Log(lng("W_In", "Warning in") + " " + procFile + ": " + lng("W_NoImageTime", "No image time in EXIF -> Using File date."), Color.Orange); } // ----- Process date ----- if (date != DateTime.MinValue) { if (item.TimeShift != TimeSpan.Zero) { date += item.TimeShift; if (set.WriteShiftedTime) { rewriteExif = true; } } } else { Log(lng("W_In", "Warning in") + " " + procFile + ": " + lng("W_NoValidImageTime", "No valid image time."), Color.Orange); } // ----- Add File to list ----- PhotoList.Add(new PhotoFile(file, item.Name, item.TimeShift, date, rewriteExif)); } else if (ext == ".mov" || ext == ".mp4" || ext == ".mkv" || ext == ".avi" || ext == ".flv" || ext == ".webm" || ext == ".wmv" || ext == ".asf" || ext == ".mpg" || ext == ".mpeg" || ext == ".m4v" || ext == ".3gp") { rewriteExif = false; dateImgStat status; date = GetMovDate(file, out status); // read file date if (status == dateImgStat.File) { Log(lng("W_In", "Warning in") + " " + procFile + ": " + lng("W_NoMovTime", "No movie time in MediaInfo -> Using File date."), Color.Orange); } // ----- Process date ----- if (date != DateTime.MinValue) { if (item.TimeShift != TimeSpan.Zero) { date += item.TimeShift; //if (set.WriteShiftedTime) rewriteExif = true; } } else { Log(lng("W_In", "Warning in") + " " + procFile + ": " + lng("W_NoValidImageTime", "No valid image time."), Color.Orange); } // ----- Add File to list ----- PhotoList.Add(new PhotoFile(file, item.Name, item.TimeShift, date, rewriteExif)); } // ----- Show progress ----- progress += progresInc; worker.ReportProgress((int)progress); // Show progress } } catch (Exception Err) { Log(lng("E_In", "Error in") + " " + procFile + ": " + Err.Message, Color.Red); } } // ----- Check if found photos ----- if (PhotoList.Count == 0) { Log(lng("W_NoPhotos", "Warning: No photos found!"), Color.Orange); return; } // ----- Sorting ----- Log(lng("Sorting", "Sorting") + "... (" + PhotoList.Count.ToString() + ")", Color.Black); PhotoList = PhotoList.OrderBy(o => o.Date).ToList(); progress = progSearch; worker.ReportProgress((int)progress); // Show progress // ----- Delete destination folder ----- if (set.ClearFolder) { string[] destFileList = System.IO.Directory.GetFiles(txtDestFolder.Text); foreach (string item in destFileList) { System.IO.File.Delete(item); } } // ----- Copy ----- Log(lng("Copying", "Copying") + "...", Color.Black); progresInc = (100.0f - progSearch) / PhotoList.Count; for (int i = 0; i < PhotoList.Count; i++) { try { // ----- Get Destination path ----- string destPath = set.DestFolder + System.IO.Path.DirectorySeparatorChar + Format(set.FileMask, i + 1, System.IO.Path.GetFileName(PhotoList[i].Path), PhotoList[i].Folder, PhotoList[i].Date); // ----- Create path if not exist ---- string pathDir = System.IO.Path.GetDirectoryName(destPath); if (!System.IO.Directory.Exists(pathDir)) { System.IO.Directory.CreateDirectory(pathDir); } // ----- If write exif -> set new date ----- if (PhotoList[i].RewriteExif) { SetImgDate(PhotoList[i].Path, PhotoList[i].Date, destPath); } else { System.IO.File.Copy(PhotoList[i].Path, destPath, true); } // ----- If update file date -> update date from EXIF ----- if (set.SetImgTime) { if (PhotoList[i].Date != DateTime.MinValue) { System.IO.File.SetCreationTime(destPath, PhotoList[i].Date); System.IO.File.SetLastWriteTime(destPath, PhotoList[i].Date); } else { Log(lng("W_In", "Warning in") + " " + System.IO.Path.GetFileName(destPath) + ": " + lng("W_TimeNotSaved", "File time from EXIF not saved (no time in EXIF).") + "", Color.Orange); } } // ----- Show progress ----- progress += progresInc; worker.ReportProgress((int)progress); // Show progress } catch (Exception Err) { Log(lng("E_In", "Error in") + " " + System.IO.Path.GetFileName(PhotoList[i].Path) + ": " + Err.Message, Color.Red); } } Log(lng("CopyDone", "Copying done") + " (" + PhotoList.Count.ToString() + " " + lng("Photos", "photos") + ")", Color.Black); worker.ReportProgress(100); }