public void Clean(CleanRule rule) { DirectoryTraverser dir = new DirectoryTraverser(rootFolder, filenameMask); foreach (FileInfo file in dir.files) { try { if (rule(file)) { file.Delete(); } } catch (Exception) { } } }
private void ConfirmButtonClick(object sender, EventArgs e) { if (Globals.RenameRules == null) { Globals.RenameRules = new List <IRenameRule>(); } switch (RuleTypeTabControl.SelectedIndex) { case 0: InsertRule insertRule = new InsertRule(insertText: InsertTextTextBox.Text, numberSequence: InsertNumberSeqRadioButton.Checked, numberSequenceStart: (int)InsertNumberSeqStartingAtNumericUpDown.Value, numberSequenceLeadingZeroes: (int)InsertNumberSeqLeadingZeroesNumericUpDown.Value, beforeTextStr: InsertBeforeTextTextBox.Text, afterTextStr: InsertAfterTextTextBox.Text, prefix: InsertPrefixRadioButton.Checked, suffix: InsertSuffixRadioButton.Checked, position: InsertAtPositionRadioButton.Checked, positionRightToLeft: InsertPositionRightLeftCheckBox.Checked, beforeText: InsertBeforeTextRadioButton.Checked, afterText: InsertAfterTextRadioButton.Checked, replaceFileName: ReplaceCurrentFileNameRadioButton.Checked, ignoreExtension: InsertIgnoreExtensionCheckBox.Checked, positionIndex: (int)InsertPositionNumericUpDown.Value); if (editMode) { int oldRuleIndex = Globals.RenameRules.IndexOf(Globals.RenameRules.Find(rule => rule.Id == currentlyEditedRule.Id)); Globals.RenameRules[oldRuleIndex] = insertRule; } else { Globals.RenameRules.Add(insertRule); } DialogResult = DialogResult.OK; break; case 1: DeleteRule deleteRule = new DeleteRule(fromPosition: DeleteFromPositionRadioButton.Checked, fromDelimiter: DeleteFromDelimiterRadioButton.Checked, toPosition: DeleteToPositionRadioButton.Checked, toDelimiter: DeleteToDelimiterRadioButton.Checked, deleteToEnd: DeleteToEndRadioButton.Checked, deleteEntireFileName: DeleteEntireFileNameCheckBox.Checked, ignoreExtension: DeleteIgnoreExtensionCheckBox.Checked, keepDelimiters: DeleteKeepDelimitersCheckBox.Checked, fromPositionIndex: (int)DeleteFromPositionNumericUpDown.Value, toPositionIndex: (int)DeleteToPositionNumericUpDown.Value, fromDelimiterStr: DeleteFromDelimiterTextBox.Text, toDelimiterStr: DeleteToDelimiterTextBox.Text); if (editMode) { int oldRuleIndex = Globals.RenameRules.IndexOf(Globals.RenameRules.Find(rule => rule.Id == currentlyEditedRule.Id)); Globals.RenameRules[oldRuleIndex] = deleteRule; } else { Globals.RenameRules.Add(deleteRule); } DialogResult = DialogResult.OK; break; case 2: RemoveRule removeRule = new RemoveRule(removeText: RemoveTextTextBox.Text, allOccurrences: RemoveAllOccurrencesRadioButton.Checked, firstOccurrence: RemoveFirstOccurrenceRadioButton.Checked, lastOccurrence: RemoveLastOccurrenceRadioButton.Checked, caseSensitive: RemoveCaseSensitiveCheckBox.Checked, ignoreExtension: RemoveIgnoreExtensionCheckBox.Checked ); if (editMode) { int oldRuleIndex = Globals.RenameRules.IndexOf(Globals.RenameRules.Find(rule => rule.Id == currentlyEditedRule.Id)); Globals.RenameRules[oldRuleIndex] = removeRule; } else { Globals.RenameRules.Add(removeRule); } DialogResult = DialogResult.OK; break; case 3: ReplaceRule replaceRule = new ReplaceRule(findText: ReplaceFindTextBox.Text, replaceText: ReplaceTextTextBox.Text, allOccurrences: ReplaceAllOccurrencesRadioButton.Checked, firstOccurrence: ReplaceFirstOccurrenceRadioButton.Checked, lastOccurrence: ReplaceLastOccurrenceRadioButton.Checked, caseSensitive: ReplaceCaseSensitiveCheckBox.Checked, ignoreExtension: ReplaceIgnoreExtensionCheckBox.Checked ); if (editMode) { int oldRuleIndex = Globals.RenameRules.IndexOf(Globals.RenameRules.Find(rule => rule.Id == currentlyEditedRule.Id)); Globals.RenameRules[oldRuleIndex] = replaceRule; } else { Globals.RenameRules.Add(replaceRule); } DialogResult = DialogResult.OK; break; case 4: CleanRule cleanRule = new CleanRule(cleanLatinAlphabet: CleanLatinAlphabetCheckBox.Checked, cleanDigits: CleanDigitsCheckBox.Checked, cleanBrackets: CleanBracketsCheckBox.Checked, cleanSymbols: CleanSymbolsCheckBox.Checked, cleanUserDefined: CleanUserDefinedCheckBox.Checked, cleanUserDefinedText: CleanUserDefinedTextBox.Text, caseSensitive: CleanCaseSensitiveCheckBox.Checked, ignoreExtension: CleanIgnoreExtensionCheckBox.Checked ); if (editMode) { int oldRuleIndex = Globals.RenameRules.IndexOf(Globals.RenameRules.Find(rule => rule.Id == currentlyEditedRule.Id)); Globals.RenameRules[oldRuleIndex] = cleanRule; } else { Globals.RenameRules.Add(cleanRule); } DialogResult = DialogResult.OK; break; case 5: RegexRule regexRule = new RegexRule(regexFindText: RegexFindTextTextBox.Text, replaceText: RegexReplaceTextTextBox.Text, ignoreExtension: CleanIgnoreExtensionCheckBox.Checked ); if (editMode) { int oldRuleIndex = Globals.RenameRules.IndexOf(Globals.RenameRules.Find(rule => rule.Id == currentlyEditedRule.Id)); Globals.RenameRules[oldRuleIndex] = regexRule; } else { Globals.RenameRules.Add(regexRule); } DialogResult = DialogResult.OK; break; default: MessageBox.Show("An unknown error has occurred.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); DialogResult = DialogResult.Cancel; break; } }