It would help if you specified the "text". But here is a skeleton of a couple approaches. For very simple (exact text) replacements, use the Non-Regex version. For even slightly more complex, use the Regex version. Your work is defining a pattern that works for you. The pattern in my Regex will remove all blank lines. A blank line is any line containing 0 or more whitespace characters. I am using delegates to switch between the Regex and Non-Regex approaches. You can greatly simplify by deciding what you want to do...
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
FixHtmlRegex(fbd.SelectedPath);
}
private void FixHtmlRegex(string dir)
{
string pattern = @"^\s*?$";
Func<string, string> doReplace = delegate(string val)
{
return Regex.Replace(val, pattern, "", RegexOptions.Multiline);
};
Predicate<string> isMatch = delegate(string val)
{
return Regex.IsMatch(val, pattern, RegexOptions.Multiline);
};
FixHtml(dir, isMatch, doReplace);
}
private void FixHtml(string dir)
{
Func<string, string> doReplace = delegate(string val)
{
return val.Replace("\t\r\n", "");
};
Predicate<string> isMatch = delegate(string val)
{
return val.Contains("\t\r\n");
};
FixHtml(dir, isMatch, doReplace);
}
private void FixHtml(string dir, Predicate<string> isMatch, Func<string,string> doReplace)
{
string[] names = new string [0];
names = Directory.GetFiles(dir, "*.htm");
foreach (string name in names)
{
string html = File.ReadAllText(name);
if (isMatch(html))
{
string newHtml = doReplace(html);
if (Directory.Exists(Path.Combine(dir, "BackupFiles")) == false)
{
Directory.CreateDirectory(Path.Combine(dir, "BackupFiles"));
}
File.Move(name, Path.Combine(Path.Combine(dir, "BackupFiles"), Path.GetFileName(name)));
File.WriteAllText(name, newHtml);
}
}
}
}
Les Potter, Xalnix Corporation,
Yet Another C# Blog