C # Beginners-Text Replacement tool command line version
Source: Internet
Author: User
Beginners | command line The program uses regular expressions for text substitution, breadth-first traversal subdirectory (basic knowledge is important), resolving the problem of unable to replace the carriage return line
Class Replacee
{
<summary>
Replacing characters in a file
</summary>
<param name= "Filefullname" > File Full name </param>
<param name= "Replacedby" > string used for substitution </param>
<param name= "FindPattern" > string used for lookup </param>
<param name= "Isbackup" > whether to back up files </param>
private static void Doreplace (String filefullname, String Replacedby, String findpattern, bool isbackup)
{
string result = String. Empty;
String inputtext = String. Empty;
string replacement = Replacedby;
string Pat = FindPattern;
Regex r = new Regex (PAT, regexoptions.ignorecase);
Try
{
using (StreamReader sr = new StreamReader (filefullname))
{
Inputtext = Sr. ReadToEnd ();
}
Add some text to the file.
using (StreamWriter sw = new StreamWriter (filefullname))
{
Sw. Write (result);
}
}
Console.WriteLine (Filefullname);
}
catch (Exception e)
{
Console.WriteLine ("The process failed: {0}", e.tostring ());
Throw (e);
}
}
<summary>
Traverse Directory
</summary>
<param name= "path" > Start path </param>
<param name= "Replacedby" > string used for substitution </param>
<param name= "FindPattern" > string used for lookup </param>
<param name= "Isbackup" > whether to back up files </param>
<param name= "Isgetsubfloder" > whether to get subfolders </param>
public static void Travelreplace (string path, String replacedstr, String findpattern, bool isbackup, BOOL Isgetsubfloder)
{
Queue queue = new Queue ();
DirectoryInfo di = null;
String subpath = String. Empty;
String Currentpath = String. Empty;
filesysteminfo[] dirs = null;
Queue. Enqueue (path);
while (queue. Count > 0)
{
Currentpath = (string) queue. Dequeue ();
DI = new DirectoryInfo (Currentpath);
Get files under current Directiory
filesysteminfo[] files = di. GetFiles ("*.sql");
foreach (FileSystemInfo f in Files)
{
Doreplace (F.fullname, Replacedstr, FindPattern, isbackup);
}
Get only subdirectories
if (Isgetsubfloder = = True)
{
dirs = di. GetDirectories ();
foreach (FileSystemInfo d in dirs)
{
Subpath = D.fullname;
Queue. Enqueue (subpath);
Console.WriteLine (subpath);
}
}
}
}
}
Test:
Replacee.travelreplace (@ "E:\temp\ttt", "\ r \ n);", @ "(\ r \ n) {2,}\);", true, true;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service