public string Process(string inputString, bool isFilename) { // get arguments // split string name = inputString; string extension = ""; if (isFilename) { name = Path.GetFileNameWithoutExtension(inputString); extension = inputString.Remove(0, name.Length); } // process name = StringProcess.MakeStringNormalized(name); // combine and return string result = name + extension; return(result); }
// rename foldername private void BatchFolderButton_Click(object sender, RoutedEventArgs e) { // show message box warns user about options selected such as: make new name or skip, ... string message = "Some final result will be different with preview result due to options\n" + "Are you sure to continue?"; string caption = "Warning"; MessageBoxButton buttons = MessageBoxButton.OKCancel; MessageBoxImage icon = MessageBoxImage.Warning; MessageBoxResult result = MessageBox.Show(message, caption, buttons, icon); if (result == MessageBoxResult.OK) { Log.Information("BatchFolderButton_Click: Init"); foreach (var foldername in foldernameList) { // each foldername string newFoldername = foldername.Value; foldername.BatchState = "Success"; foldername.FailedActions = "Failed Actions List:\n"; bool isSuccess = true; // apply all actions to foreach (var action in addList) { try { newFoldername = action.Process(newFoldername, false); } catch (Exception ex) { isSuccess = false; foldername.FailedActions += action.Description + "\n"; //MessageBox.Show("Preview Foldername Exception: " + ex.Message); Log.Error("BatchFolderButton_Click: " + ex.Message); } } if (!isSuccess) { foldername.BatchState = "Fail"; } // after getting new foldername, we rename foldername foldername.NewFoldername = newFoldername; try { string oldFoldername = foldername.Path + foldername.Value; newFoldername = foldername.Path + foldername.NewFoldername; // if foldername already existed if (newFoldername.ToLower() != oldFoldername.ToLower() && Directory.Exists(newFoldername)) { if (NameCollisionOption == OptionsWindow.NewName) { // make new foldername int count = 1; string sample = newFoldername; while (Directory.Exists(newFoldername)) { string difference = " - " + count.ToString(); newFoldername = StringProcess.MakeNewFoldername(sample, difference); count++; } // rename // generate a GUID Guid g = Guid.NewGuid(); string guidString = g.ToString(); string oldFoldernameGUID = foldername.Path + guidString; System.IO.Directory.Move(oldFoldername, oldFoldernameGUID); System.IO.Directory.Move(oldFoldernameGUID, newFoldername); } else if (NameCollisionOption == OptionsWindow.Skip) { // do nothing } } else // if foldername doesnot exist, just rename { //System.IO.Directory.Move(oldFoldername, newFoldername); // rename // generate a GUID Guid g = Guid.NewGuid(); string guidString = g.ToString(); string oldFoldernameGUID = foldername.Path + guidString; System.IO.Directory.Move(oldFoldername, oldFoldernameGUID); System.IO.Directory.Move(oldFoldernameGUID, newFoldername); } } catch (Exception ex) { //MessageBox.Show(ex.Message); Log.Error("BatchFolderButton_Click: " + ex.Message); } } Log.Information("BatchFolderButton_Click: End"); } }
public void LoadImageName() { // open folder, get all filenames //AppDomain.CurrentDomain.BaseDirectory string path = AppDomain.CurrentDomain.BaseDirectory + foldername; string[] filenames = Directory.GetFiles(path); // check filename to assign to variables foreach (var fullname in filenames) { string filename = Path.GetFileName(fullname); string[] tokens = StringProcess.SplitString(filename, new string[] { "_" }); if (tokens[0].ToLower() == "Application".ToLower()) { ApplicationIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Option".ToLower()) { OptionIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Remove".ToLower()) { RemoveIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "RemoveAll".ToLower()) { RemoveAllIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Add".ToLower()) { AddIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Preview".ToLower()) { PreviewIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Start".ToLower()) { StartIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Refresh".ToLower()) { RefreshIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Help".ToLower()) { HelpIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Open".ToLower()) { OpenIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Save".ToLower()) { SaveIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Information".ToLower()) { InformationIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "File".ToLower()) { FileIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Folder".ToLower()) { FolderIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "Action".ToLower()) { ActionIcon.Add ( new MyString() { Value = path + filename } ); } else if (tokens[0].ToLower() == "List".ToLower()) { ListIcon.Add ( new MyString() { Value = path + filename } ); } } }