private void Worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < db.Count(); i++) { FilesToMove dir = db.GetDirInfo(i); while (dir.Count() > 0) { if (worker.CancellationPending == true) { e.Cancel = true; return; } string file = dir.GetFile(0); //The file to move string destination = dir.GetDirectory() + "\\" + Path.GetFileName(dir.GetFile(0)); //The destination folder try { db.MoveFile(file, i); } catch { } IntCurrentFile++; int prog = Convert.ToInt32(((double)IntCurrentFile / (double)db.IntTotalFiles) * 100); (sender as BackgroundWorker).ReportProgress(prog, IntCurrentFile); //pass along the actual progress as well as the numerical amount of files that have been moved Thread.Sleep(300); } } }
/// <summary> /// Adds a directory to db, if it doesn't exist /// </summary> /// <param name="dir"></param> public bool AddDirectory(string directory) { FilesToMove dir = new FilesToMove(directory); //If we get a valid directory to drop things in, we need to create a new directory, //as well as a list to hold what files go to that directory if (!Directories.Contains(dir) && Directory.Exists(directory)) { Directories.Add(dir); Directories.Sort(); return(true); } return(false); }
/// <summary> /// Loads a json file into the database, if it is valid. /// </summary> /// <param name="file"></param> /// <returns>True on a successful load, false otherwise</returns> public bool LoadJSon(string file) { if (File.Exists(file)) { using (StreamReader reader = File.OpenText(file)) { JObject jFile = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); if (jFile.ContainsKey("SourceFiles") && jFile.ContainsKey("Directories")) { JArray sourceArray = JArray.Parse(jFile["SourceFiles"].ToString()); foreach (JToken token in sourceArray) { SourceFiles.Add(token.ToString()); } JObject dirObj = JObject.Parse(jFile["Directories"].ToString()); Console.WriteLine(dirObj.Count); foreach (JContainer obj in dirObj.Children()) { JProperty property = obj.ToObject <JProperty>(); FilesToMove dir = new FilesToMove(property.Name); JArray filesArray = JArray.Parse(property.Value.ToString()); foreach (JToken token in filesArray) { dir.Add(token.ToString()); IntTotalFiles++; } Directories.Add(dir); } } else { return(false); } } return(true); } return(false); }
private void MoveHandler(object sender, RoutedEventArgs e) { if (db.Count() == 0) //TODO: Refactor so any and all GUI calls are handled elsewhere { MessageBox.Show("No files to move."); return; } MessageBoxResult result = MessageBox.Show("Are you sure you want to move the files?", "Move all files?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); if (result == MessageBoxResult.No) { return; } bool OverwriteAll = false; bool NoToAll = false; for (int i = 0; i < db.Count(); i++) //we initially check for duplicate files or invalid directories { FilesToMove dir = db.GetDirInfo(i); List <string> removedFiles = new List <string>(); if (!Directory.Exists(db.GetDirInfo(i).GetDirectory())) { MessageBox.Show("Error: directory " + dir.GetDirectory() + " not found. Was this from an old save?", "Error", MessageBoxButton.OK, MessageBoxImage.Error); db.RemoveDirectory(i); continue; } foreach (string file in dir.GetFiles()) { if (!File.Exists(file)) { MessageBox.Show("Error: source file " + file + " not found. Was this from an old save?", "Error", MessageBoxButton.OK, MessageBoxImage.Error); removedFiles.Add(file); continue; } string destination = dir.GetDirectory() + "\\" + Path.GetFileName(file); if (File.Exists(destination) && !OverwriteAll) //file already exists at destination, and no overwrite flag set { FileCompare compare = new FileCompare(file, destination); compare.ShowDialog(); if (compare.RESULT == Result.YESTOALL) { OverwriteAll = true; } else if (compare.RESULT == Result.NO) { removedFiles.Add(file); } else if (compare.RESULT == Result.NOTOALL) { NoToAll = true; removedFiles.Add(file); } else if (compare.RESULT == Result.KEEPBOTH) { string ext = Path.GetExtension(file); string filename = Path.GetFileNameWithoutExtension(file); //First we need the amount of duplicates that are in the folder //Since we'll be off by one due to the original file not including a (x), we add one int offset = (Directory.GetFiles(dir.GetDirectory(), filename + " (?)" + ext)).Length + 2; destination = dir.GetDirectory() + "\\" + Path.GetFileNameWithoutExtension(file) + " (" + offset + ")" + ext; File.Move(file, Path.GetFullPath(file) + Path.GetFileName(destination)); //Renames file if (!dir.UpdateFileName(file, destination)) //Attempts to update the file name within the db { MessageBox.Show("Unexpected Error, something went wrong trying to keep both files. It may not exist.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else if (compare.RESULT == Result.CANCEL) { return; } } else if (File.Exists(destination) && NoToAll) { removedFiles.Add(file); } } } DisableButtons(); HidePreview(); imgPreview.Source = null; listViewDestination.ItemsSource = null; worker.RunWorkerAsync(); }