private void RenameFiles(RenameModel model) { newFiles = new List <string>(); RenamerEngine renamer = new RenamerEngine(model.FilenamePattern.Contains("%#"), !model.FilenamePattern.Contains("%##")); files.Sort(); foreach (string filename in files) { // get the new name form the metadata PictureMetaData pmd = new PictureMetaData(filename); string newname = FileNameFormater.FormatFilename(pmd, model.FilenamePattern); newname = newname.Replace("%##", "%#"); pmd.Close(); if (newname != "") { // open a file info FileInfo fi = new FileInfo(filename); newname = fi.DirectoryName + "\\" + newname; // give the new name to the renamer renamer.AddNewRenameItem(fi, newname); } else { newFiles.Add(filename); } // next this.FireOneFileProcessed(); } // and now: do all the dirty working renamer.CopyMove(true); // create the list of new files foreach (string filename in renamer.GetAllNewFilenames()) { if (File.Exists(filename)) { newFiles.Add(filename); } } }
private void RenameDirectories(RenameModel model) { // find all directories to rename List <string> directories = new List <string>(); foreach (string file in files) { FileInfo fi = new FileInfo(file); string dir = fi.Directory.FullName; if (!directories.Contains(dir)) { directories.Add(dir); } } // compute the new names for these directories RenamerEngine renamer = new RenamerEngine(model.DirectoryPattern.Contains("%#"), !model.DirectoryPattern.Contains("%##"), false); foreach (string directory in directories) { DirectoryInfo di = new DirectoryInfo(directory); string newName = GetNewDirectoryname(di, model.DirectoryPattern); if (newName != "") { renamer.AddNewRenameItem(di, newName); } } renamer.CopyMove(true); rootDirectory = renamer.GetNewName(rootDirectory); // update the new files List <string> tempFiles = new List <string>(); foreach (KeyValuePair <string, string> oldAndNewFilename in renamer.GetAllOldAndNewFilenames()) { string oldDirectory = oldAndNewFilename.Key.ToLower(); string newDirectory = oldAndNewFilename.Value.ToLower(); if (oldDirectory != newDirectory) { for (int i = 0; i < newFiles.Count; i++) { if (newFiles[i].ToLower().StartsWith(oldDirectory)) { string newFile = newDirectory + "\\" + newFiles[i].Substring(oldDirectory.Length); newFile = newFile.Replace("\\\\", "\\"); newFiles[i] = newFile; tempFiles.Add(newFile); } } } } for (int i = 0; i < newFiles.Count; i++) { if (!File.Exists(newFiles[i])) { newFiles.RemoveAt(i); i--; } } foreach (string file in files) { if (File.Exists(file) && !newFiles.Contains(file)) { newFiles.Add(file); } } foreach (string file in tempFiles) { if (File.Exists(file) && !newFiles.Contains(file)) { newFiles.Add(file); } } //TODO debugging Debug.Assert(this.files.Count == this.newFiles.Count, "Anzahl an Dateien nach Rename falsch.", string.Format("vorher {0} nachher {1}", this.files.Count, this.newFiles.Count)); }