2017-03-03 88 views
1

記錄我的工作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 

而且,這是一個需要分割文本文件的截屏。 Text file snap shot

在此先感謝。

回答

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 
+0

我dont't要使用ReadAllLines改變Identifier = Mid(sr.ReadLine, 1, 5)。怎麼一回事,因爲它加載在內存中的文件和可能是內存問題。這可能與StreamReader的 –

+0

@VirenderThakur我修訂我的答案。 – MrGadget

0

原來的答覆在這個例子中我做了多個文本文件。我希望能有所幫助。

p.s.在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