Vb.net 在文本文件中拆分记录

Vb.net 在文本文件中拆分记录,vb.net,console,Vb.net,Console,我正在开发vb.net应用程序。其中我有多个文本文件,需要根据文件中的某个标识符(重复单词)拆分记录。 你能帮我一下吗?我是vb.net新手,不知道怎么做。 到目前为止,我已经编码了 If (Directory.Exists(filePath)) Then 'search file in the input path by their search pattern For Each File As String In Directory.GetFi

我正在开发vb.net应用程序。其中我有多个文本文件,需要根据文件中的某个标识符(重复单词)拆分记录。 你能帮我一下吗?我是vb.net新手,不知道怎么做。 到目前为止,我已经编码了

If (Directory.Exists(filePath)) Then
            '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 statementDate As String
                    Dim currenttext As String

                    'getting the unique identifier from the files and removing the white spaces
                    Identifier = sr.ReadLine.Substring(69, 8)
                    'checks until the EOF
                    While Not sr.EndOfStream

                        currenttext = sr.ReadLine()
                        'loop through until identified not repeated
                        Do Until currenttext.Contains(Identifier)

                            Currentline = sr.ReadLine()
                            Console.WriteLine(Currentline)


                        Loop
                        Console.WriteLine("=========================== Records Ends")

                    End While
                End Using
另外,这里是需要拆分的文本文件的屏幕截图


提前谢谢

这应该适合你

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
原始答案如下


这应该对你有用

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
原始答案如下


在本例中,我创建了多个文本文件。我希望能有所帮助

p、 在
Identifier=Mid(sr.ReadLine,1,5)
in
Identifier=Mid(sr.ReadLine,69,8)

再见


在本例中,我创建了多个文本文件。我希望能有所帮助

p、 在
Identifier=Mid(sr.ReadLine,1,5)
in
Identifier=Mid(sr.ReadLine,69,8)

再见


我不想使用ReadAllLines。因为它在内存中加载文件,可能是内存问题。这有可能吗streamreader@VirenderThakur我已经修改了我的答案。我不想使用ReadAllLines。因为它在内存中加载文件,可能是内存问题。这有可能吗streamreader@VirenderThakur我已经修改了我的答案。
 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