0

我有以下基本代码,它从文本文件中提取所有数据并将其存入列表框(zMailbox 在类的顶部声明)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim finfo As New IO.DirectoryInfo(zMailbox)

For Each fi In finfo.GetFiles("*.txt")

ListBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(fi.Name)) 'filename only

Next

End Sub

文本框中包含的信息如下所示...

    Unit Name = 
    Unit Code = 
    Operation =  
    Requirements = 
    Last Audit Date =
    Last Auditor = 
    Date Planned = 

在文本文件所在的文件夹中,最多可以有 20 个不同的文本文件,但数据不同。我正在努力调整代码以查看所有文本文件,如果“Date Planned =”行与从日期选择器中选择的日期匹配,则复制并在“=”行之后附加信息 Unit代码、单元名称、操作和应用于列表框

任何建议将不胜感激

4

1 回答 1

1

一个简单的解决方案,可以相当容易地扩展。

它是如何工作的?

  1. 按下按钮 -> 从路径中获取的 txt 文件名
  2. 打开每个txt文件,逐行读取
  3. 每行在“=”上分割并插入到数据对象中,以便可以查询
  4. 然后我们在该数据对象列表中搜索我们的值并返回整个文件的内容
  5. 文件内容输出到控制台

代码:

Public Class Form1
    Dim zMailbox As String = "C:\Windows\Temp\Test"
    Dim allFiles As New List(Of String)
    Dim allFileContents As New List(Of FileLine)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'grab list of all txt files
        allFiles = fetchFileNames(zMailbox)
        'read all of the text files and tidy the data
        processFiles(allFiles)
        'search the collected data for 'Date Planned'
        Dim fileContents As List(Of FileLine) = searchCleanData("Date Planned")
        'now we have our file with 'Date Planned' in write all of the lines out to the console.
        If Not fileContents Is Nothing AndAlso fileContents.Count > 0 Then
            'output the filepath so we know which file we are looking at
            Console.WriteLine("File: " & fileContents(0).FilePath)
            For Each line In fileContents
                Console.WriteLine(line.Title & " - " & line.Val)
            Next
        End If
    End Sub
    ''' <summary>
    ''' use linq to find a single file in a list of files that matches a criteria
    ''' </summary>
    ''' <param name="searchText"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function searchCleanData(ByVal searchText As String) As List(Of FileLine)
        Dim returnVar As New List(Of FileLine)
        'find the first filename that contains the value
        Dim fileName = (From line In allFileContents
                    Where line.Title = searchText
                    Select line.FilePath).FirstOrDefault
        'if there is a filename that matches the value return it's contents
        If Not fileName Is Nothing AndAlso Not fileName Is String.Empty Then
            returnVar = (From line In allFileContents
                        Where line.FilePath = fileName
                        Select line).ToList
            Return returnVar
        End If
    End Function
    Private Sub processFiles(ByVal fileNames As List(Of String))
        For Each filePath In fileNames
            Dim contents As List(Of String) = fetchFileContents(filePath)
            'process the individual lines
            allFileContents = splitLines(contents, filePath)
        Next
    End Sub
    ''' <summary>
    ''' Split a list of strings on '=' 
    ''' </summary>
    ''' <param name="fileContents"></param>
    ''' <param name="filepath"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function splitLines(ByVal fileContents As List(Of String), ByVal filepath As String) As List(Of FileLine)
        'return variable
        Dim returnLines As New List(Of FileLine)
        For Each line In fileContents
            Dim currentLine As New FileLine
            'split the current line on the '=' character
            Dim splitline As String() = line.Split("=")
            'now the string should be in two parts, the first being the title, the second being the value
            currentLine.Title = (splitline(0)).Trim
            currentLine.Val = (splitline(1)).Trim
            'attach the filepath so we can search by it
            currentLine.FilePath = filepath
            'add to return var
            returnLines.Add(currentLine)
        Next
        Return returnLines
    End Function
    ''' <summary>
    ''' Search dir for txt files and return all filenames
    ''' </summary>
    ''' <param name="directory"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function fetchFileNames(ByVal directory As String) As List(Of String)
        Dim allFiles As New List(Of String)
        Dim finfo As New IO.DirectoryInfo(zMailbox)
        For Each fi In finfo.GetFiles("*.txt")
            'add the filepath to our list of paths to process
            allFiles.Add(fi.FullName)
            ListBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(fi.Name)) 'filename only
        Next
        Return allFiles
    End Function
    ''' <summary>
    ''' Read a textfile line by line to a list of strings
    ''' </summary>
    ''' <param name="fullFilePath"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function fetchFileContents(ByVal fullFilePath As String) As List(Of String)
        Dim allLines As List(Of String) = New List(Of String)
        Try
            Dim reader As New System.IO.StreamReader(fullFilePath)
            Do While Not reader.EndOfStream
                allLines.Add(reader.ReadLine())
            Loop
            reader.Close()
        Catch ex As Exception
            Return Nothing
        End Try
        Return allLines
    End Function

End Class
Public Class FileLine
    Public Property FilePath As String
    Public Property Title As String
    Public Property Val As String
End Class
于 2013-11-03T11:03:45.640 回答