根据日期返回文本文件,然后将某些行返回到列表框

时间:2013-11-02 21:40:21

标签: vb.net visual-studio-2010

我有以下基本代码从文本文件中提取所有数据并将其存入列表框(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 =”行匹配从日期选择器中选择的日期,那么复制并在行“=”后面附加信息代码,单位名称,操作并应用于列表框

将非常感激地收到任何建议

1 个答案:

答案 0 :(得分:1)

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

它是如何运作的?

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

    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