如何将.log文件中的某些文本复制到文本文件中? [英] How do I copy some text in .log file to text file?

查看:183
本文介绍了如何将.log文件中的某些文本复制到文本文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Dev的新手。我想知道将内容从一个文本文件复制到另一个文本文件或Excel的代码,其中源文件保存为.log文件。



大小该文件是42000+ KB。



我的要求是我需要使用Error关键字搜索日志文件,并且需要获取第1行和最后一行(包含错误信息)。



我想在Windows中使用文本框(接受文件路径)和按钮开发代码。



文本样本如下:



2> BUILDMSG模板:地图:阅读元数据文件:000000000.xml

2> BUILDMSG模板:地图:创建新的模板对象...

2> BUILDMSG模板:地图:确保存在模板二进制文件:P:\Import \ x64 \ship \ officetemplates\x-none \ ocpdata \ lt -t \\ _templates \

2> BUILDMSG模板:地图:错误访问P:\ Import \\ \\x64\ship\officet emplates\x-none \ ocpdata \ ltt-lt \ templates \!

2> BUILDMSG模板:地图:跳过的模板 - 没有二进制文件



任何人请帮助我..

I'm new to Dev. I would like to know the code for copying contents from one text file to another text file or Excel where the source file is saved as .log file.

The size of the file is 42000+ KB.

My requirement is I need to sear the log file with "Error" keyword and need to get the 1st and last line(which contains Error message).

I want to develop code in Windows from with a text box(accepts the file path) and a button.

Sample of the text is given below

2>BUILDMSG TEMPLATES:Maps: Reading MetaData File: 000000000.xml
2>BUILDMSG TEMPLATES:Maps: Creating new Template object...
2>BUILDMSG TEMPLATES:Maps: Ensuring template binary exists: P:\Import\x64\ship\officetemplates\x-none\ocpdata\lt-lt\templates\
2>BUILDMSG TEMPLATES:Maps: Error when accessing P:\Import\x64\ship\officetemplates\x-none\ocpdata\lt-lt\templates\!
2>BUILDMSG TEMPLATES:Maps: Skipped template - No Binary File

Any one please help me..

推荐答案

如何绕线,逐行直到最后一行?

http://msdn.microsoft.com/en-us /library/aa287535(v=vs.71).aspx [ ^ ]
How about loop through the lines, line by line until the last line?
http://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx[^]


首先你可以使用文本属性 [ ^ ]。通过这种方式,您可以获得用户放入的文件路径。 不要忘记来验证用户输入,因为它可以是空字符串或不是现有文件。



之后你可以使用 FileStream [ ^ ]或 StreamReader [ ^ ]以便从路径中读取您的文件。



您可以从文件中读取行并检查是否实际行包含您的关键字如下:

First of all you can use Text property[^] of TextBox control in a windows form application. With this you can achive the file path what user puts into that. Do not forget to validate the user input because it can be an empty string or not existing file.

After you can use FileStream[^] or StreamReader[^] in order to read your file from path.

After you can read lines from file and check if the actual line is containing your keyword as the follow:
string condition = "ERROR";

while ((line = reader.ReadLine()) != null)
{
    if (line.Contains(condition))
    {
        //here you can process the line as you want
    }
}





或者,如果您需要第一行和最后一行,您可以执行以下操作:



Or if you need the first and the last lines you can do this as follow:

string firstLine = string.Empty,
       lastLine = string.Empty;

while ((line = reader.ReadLine()) != null)
{
    if (firstLine.Equals(string.empty))
    {
        //this will updated only in the first cycle
        firstLine = line;
    }

    else
    {
        //and this will be updated in every line (for last in the last line)
        lastLine = line;
    }
}

//here you can process the firstLine and lastLine variables


这篇关于如何将.log文件中的某些文本复制到文本文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆