大家自己改进一下能做任意修改
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace AutoFilesReplace
{
public partial class AutoFilesReplace : Form
{
public AutoFilesReplace()
{
InitializeComponent();
}
private delegate void UpDateDgvDelegate(string msg);
private UpDateDgvDelegate _upDateStateDelegate;
int off = 0;
/// <summary>
/// 修改表格的行数据
/// </summary>
/// <param name="msg">要修改为的数据</param>
/// <param name="rowId">行号</param>
/// <param name="columnName">列名</param>
private void UpDateDgv(string msg)
{
try
{
richTextBox1.AppendText("\r\n" + msg.ToString());
richTextBox1.ScrollToCaret();
}
catch { }
}
private void AutoFilesReplace_Load(object sender, EventArgs e)
{
_upDateStateDelegate = new UpDateDgvDelegate(UpDateDgv);
}
private void btnEnter_Click(object sender, EventArgs e)
{
if (btnEnter.Text.Contains("开始"))
{
off = 0;
btnEnter.Text = "结束";
Thread pingTask = new Thread(new ThreadStart(delegate
{
//开始时间
this.BeginInvoke(_upDateStateDelegate, "开始");
string result = string.Empty;
ForeachFile(textBox1.Text, ref result);
//开始时间
this.BeginInvoke(_upDateStateDelegate, result);
//开始时间
this.BeginInvoke(_upDateStateDelegate, "结束");
}));
pingTask.Start();
}
else
{
off = 1;
btnEnter.Text = "开始";
}
}
static string beianpattern = "ICP备\\d{7,12}号";
static string no_beianpattern = "ICP备\\d{7,12}号-1";
/// <summary>
/// 遍历指定文件夹中的文件包括子文件夹的文件
/// </summary>
/// <param name="filePathByForeach">等待遍历的目录(绝对路径)</param>
/// <param name="result">遍历之后的结果</param>
/// <returns></returns>
public static void ForeachFile(string filePathByForeach, ref string result)
{
DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
FileInfo[] file = theFolder.GetFiles("*.html");//获取所在目录的文件
foreach (FileInfo fileItem in file) //遍历文件
{
string beianhao = string.Empty;
using (StreamReader sr = new StreamReader(fileItem.FullName, Encoding.UTF8))
{
beianhao = sr.ReadToEnd();
}
if (Regex.IsMatch(beianhao, beianpattern))
{
if (Regex.IsMatch(beianhao, no_beianpattern))
{
result += "有正常备案号的文件:" + fileItem.FullName + "\r\n";
}
else
{
try
{
string beianhaostr = Regex.Match(beianhao, beianpattern).Value.Trim();
beianhao = beianhao.Replace(beianhaostr, beianhaostr + "-1");
using (StreamWriter sw = new StreamWriter(fileItem.FullName, false, Encoding.UTF8))
{
sw.Write(beianhao);
}
}
catch (Exception ex)
{
result += ex.Message;
}
//正常的
result += "有备案号但不对的文件:" + fileItem.FullName + " 现在已经修复\r\n ";
}
}
}
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
ForeachFile(NextFolder.FullName, ref result);
}
}
}
}
|