删除文件夹中*.txt日志文件的某些文字,另存处理后的文件

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();
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值