24 September, 2010
6 Comments
1 category
If you want to find and replace some text in all files of a given folder, including subfolders, you can use the following C# code:
string rootfolder = @"C:\Temp"; string[] files = Directory.GetFiles(rootfolder, "*.*", SearchOption.AllDirectories); foreach (string file in files) { try { string contents = File.ReadAllText(file); contents = contents.Replace(@"Text to find", @"Replacement text");
// Make files writable File.SetAttributes(file, FileAttributes.Normal);
File.WriteAllText(file, contents); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
Tags: C#
Category: Uncategorized
Great code Roel, if i wanted to do multiple find and replace statements how would i do it i.e
I have the following tags i need to replace with nothing
Excellent !!!
Resolve my problem !!
Hi,
I need to do more than 1 replace…how can I do that ? I tried to add more replace commands but it is not working.
Thank you
Hi, thank you for this awesome code….it works and solves the issue…but i am using this for replacing the text for classic asp and after i run the code i am facing encoding issue…can you suggest?
Hi,
This post is old, but it really helped me
Thank you soooooo much!
worked ..Thank you so much 🙂