/// <summary> /// Executes this command. /// </summary> /// <param name="parameter">The parameter.</param> public override void Execute(object parameter, object target) { FileInfo[] files; string pattern; string replaceText; using (var fbd = new FormReplaceInFiles()) { if (fbd.ShowDialog() != DialogResult.OK) { return; } files = fbd.SelectedFiles; pattern = fbd.SearchText; replaceText = fbd.ReplaceText; } Regex regex = new Regex(pattern); AddLog("Start replace"); foreach (var file in files) { AddLog("Replace in file: " + file.FullName); string content = File.ReadAllText(file.FullName); File.WriteAllText(file.FullName, regex.Replace(content, replaceText)); } AddLog("End replace"); }
/// <summary> /// Executes this command. /// </summary> /// <param name="parameter">The parameter.</param> /// <param name="target">The target.</param> public override void Execute(object parameter, object target) { FileInfo[] files; string pattern; string replaceText; bool useRegExp; using (var fbd = new FormReplaceInFiles()) { if (fbd.ShowDialog() != DialogResult.OK) { return; } files = fbd.SelectedFiles; pattern = fbd.SearchText; replaceText = fbd.ReplaceText; useRegExp = fbd.UseRegExp; } AddLog("Start rename"); foreach (var file in files) { AddLog("Rename file: " + file.FullName); var newName = StringUtil.Replace(file.Name, pattern, replaceText, useRegExp, null); try { if (newName != file.Name) { File.Move(file.FullName, Path.Combine(file.DirectoryName, newName)); } } catch (Exception ex) { AddLog("Error rename " + ex); } } AddLog("End rename"); }