拆分文本文件中的记录

时间:2017-03-03 08:38:07

标签: vb.net console

我正在研究vb.net应用程序。其中我有多个文本文件,需要根据文件中的某些标识符(重复的单​​词)拆分记录。 你可以帮助我,因为我是vb.net的新手并且不知道如何做到这一点。 到目前为止,我已编码

{{1}}

此外,这是需要拆分的文本文件的屏幕截图。 https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html

提前致谢。

2 个答案:

答案 0 :(得分:0)

这对你有用....

Imports System.IO
Imports System.Text

Sub Main()
    If (Directory.Exists(filePath)) Then
        For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)
            Dim Record As New StringBuilder
            Dim Identifier As String = String.Empty

            Debug.Print("Reading the current file {0}", Path.GetFileName(File))
            Using sr As StreamReader = New StreamReader(File)
                While Not sr.EndOfStream
                    Dim ThisLine As String = sr.ReadLine.Trim

                    Select Case True
                        Case ThisLine.Length = 0
                            ' Skip blank lines
                        Case Identifier.Length = 0
                            ' We need to set the Identifier
                            Identifier = ThisLine
                        Case ThisLine = Identifier
                            ' We have the whole record
                            ProcessRecord(Record.ToString.Trim)

                            ' Reset for next record
                            Record.Clear()
                        Case Else
                            ' Add this line to the current record
                            Record.AppendLine(ThisLine)
                    End Select
                End While

                ' Process last record in file
                ProcessRecord(Record.ToString.Trim)
            End Using

            Debug.Print("=========================== File Ends")
        Next
    End If
End Sub

Sub ProcessRecord(Record As String)
    If Record.Length > 0 Then
        Debug.Print(Record)
        Debug.Print("=========================== Record Ends")
    End If
End Sub

以下原始答案

If (Directory.Exists(filePath)) Then
    For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)
        Dim AllLines() As String = IO.File.ReadAllLines(File)
        Dim Identifier As String = AllLines.First
        Dim Records() As String = Split(Join(AllLines, Environment.NewLine), Identifier)

        For Each Rec As String In Records
            Debug.Print(Rec)
            Debug.Print("=========================== Record Ends")
        Next
    Next

    Debug.Print("=========================== File Ends")
End If

答案 1 :(得分:0)

在这个例子中,我制作了多个文本文件。我希望能有所帮助。

P.S。更改Identifier = Mid(sr.ReadLine, 1, 5)

中的Identifier = Mid(sr.ReadLine, 69, 8)

再见

 If (Directory.Exists(filePath)) Then

        Try
            'search file in the input path by their search pattern
            For Each File As String In Directory.GetFiles(filePath, "*.txt", SearchOption.TopDirectoryOnly)

                Console.WriteLine("Reading the current file " + Path.GetFileName(File))
                Using sr As StreamReader = New StreamReader(File)
                    Dim Currentline As String = ""
                    Dim Identifier As String = ""
                    Dim currenttext As String = ""
                    Dim Prog As Integer = 0
                    Dim flg As Boolean = True

                    While Not sr.EndOfStream

                        'getting the unique identifier from the files and removing the white spaces
                        Identifier = Mid(sr.ReadLine, 1, 5)

                        Do While Not sr.EndOfStream

                            Do While flg = True
                                Currentline = sr.ReadLine()
                                If Identifier = Currentline.Trim Then
                                    Exit Do
                                ElseIf sr.EndOfStream Then
                                    currenttext = currenttext + Currentline + vbCrLf
                                    Exit Do
                                End If
                                currenttext = currenttext + Currentline + vbCrLf
                            Loop

                            currenttext = currenttext + "=========================== Records Ends"

                            Prog += 1
                            Dim objWriter As New System.IO.StreamWriter(filePath + "\" + Path.GetFileName(File) + "_" + Prog.ToString + ".txt")
                            objWriter.WriteLine(currenttext)
                            objWriter.Close()
                            currenttext = ""
                        Loop

                    End While

                End Using

            Next

            MessageBox.Show("end")

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End If