using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace DeleteVisionChecklogs
{
public class Check
{
private string _fileType = "*.txt";
public void Search(string path)
{
if (string.IsNullOrEmpty(path))
{
Console.WriteLine($"{path}为空,不执行");
return;
}
if (!Directory.Exists(path))
{
Console.WriteLine($"{path}不存在,不执行");
return;
}
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);//创建实例
DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();//获取全部子目录
for (int i = 0; i < directoryInfos.Length; i++)
{
string filePath = directoryInfos[i].FullName;//子目录路径
string[] info = Directory.GetFiles(filePath, _fileType, SearchOption.TopDirectoryOnly);//搜索文件
string subDir = filePath + @"\ProcessDir\";//定义用作存储处理后文件的文件夹
if (Directory.Exists(subDir))//存在,则清空
{
Directory.Delete(subDir, true);
}
Thread.Sleep(100);//删除文件,需要等待缓存清除延时
if (!Directory.Exists(subDir))//不存在,则创建
{
Directory.CreateDirectory(subDir);
}
foreach (var item in info)//遍历搜索出的文件
{
if (!item.Contains("Error"))//查找目标文件包含Error字符
{
continue;//继续下一循环
}
string fileName = item.Substring(item.LastIndexOf("\\")+1);//获取文件名字
fileName = subDir + fileName;//补齐新路径
//逐行读出来判断,符合条件,则逐行写入
StreamWriter sw = new StreamWriter(fileName, false);//写入时候,覆盖文件
using (StreamReader sr = new StreamReader(item))
{
string data = sr.ReadLine();
while (!string.IsNullOrEmpty(data))//读到空,说明已经读完,退出while循环
{
if (!data.Contains("PR检测(类型:DutBotLocation)识别,请检查视觉识别对象是否处于正常状态!"))
{
sw.WriteLine(data);
}
data = sr.ReadLine();//读下一行
}
}
sw.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine($"Search运行异常:{e.Message}");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeleteVisionChecklogs
{
class Program
{
static void Main(string[] args)
{
Check check = new Check();
check.Search(@"G:\rizhi\2022_02");
Console.WriteLine("press any key to exit...");
Console.ReadKey();
}
}
}