从文本文件中搜索文本框中的每个单词并添加到数据表

时间:2017-10-11 17:04:33

标签: vb.net

我需要从文本文件列表中搜索单词,并且每个单词在文本框中用空格或分号分隔。将行添加到数据表并将其显示到数据网格。问题只是表格上显示的最后一个字。提前谢谢。

    Sub test(ByVal strtofind As String)
    Dim tmp2Table As DataTable = New DataTable

    tmp2Table.Columns.Add("SN", GetType(String))
    tmp2Table.Columns.Add("Dataset", GetType(String))
    tmp2Table.Columns.Add("Param", GetType(String))
    tmp2Table.Columns.Add("Value", GetType(String))

    Dim strTemp() As String
    Dim lines() As String
    Dim strline As String = ""

    Dim fileList = Directory.GetFiles("C:\Users\sterc\Desktop\Traceview\Complete\", "*.txt", False)
    Dim sb = New StringBuilder()
    Dim result As String = String.Empty
    Dim value As String = String.Empty
    Dim s As String = txtParamSearch.Text
    Dim str As String = ""
    Dim strdataset As String = ""
    Dim strParam As String = ""
    Dim strParamResult As String = ""
    Dim strSN As String = ""

    For Each fileName In fileList
        lines = File.ReadAllLines(fileName)
        Dim intTotalLines As Integer = lines.Length
        ' Split string based on spaces.
        For intCounter = 1 To intTotalLines - 1
            strline = lines(intCounter)
            If (Regex.IsMatch(strline, "----- Test_") And Regex.IsMatch(strline, ", Started At ")) Then

                strTemp = strline.Split(" ")
                strdataset = strTemp(3).TrimEnd(",")
            End If
            If Regex.IsMatch(strline, "Reported module serial number:") Then
                strTemp = Regex.Split(strline, ": ")
                strSN = strTemp(1)
            End If
            Dim strParamtofind As String = "\b" & strtofind & "\b\s+(\w+)"

            For Each a As Match In Regex.Matches(strline, strParamtofind, RegexOptions.IgnoreCase)

                tmp2Table.Rows.Add(strSN, strdataset, a.Groups(0).Value, a.Groups(1).Value)

            Next

        Next

    Next
    DataGridView1.DataSource = tmp2Table

End Sub
Sub strArr()

    Dim s As String = txtParamSearch.Text
    Dim str As String = ""

    Dim words As String() = s.Split(New [Char]() {";"c})

    Dim word As String
    For Each word In words
        test(word)
    Next

End Sub

enter image description here

另一件事是显示整数或四舍五入。假设如下图所示(带小数点)

enter image description here

我需要从文本文件中搜索数据,每个文件都包含在下面的行中。然后在它下面是我可能在文本框中列出的数据。 让我们从我需要找到的文本框中说出来,

  • SOA_MinCurrent_EDATA;
  • Isoa_OPSL_initial

列出的两个中的每一个,都将搜索文件夹下列出的每个文本文件。然后逐行显示它们。就像上面的截图一样。问题是只显示字符串搜索并且数字是圆整的。

 Testing tune_rf_pwr-rm-f-tpt ----- Test_TxLaserPwrTune, Started At   9/16/2017 5:25:00 PM
 more lines here....
 ...................
 ---tx_laser_pwr_tune_Params    Isoa_OPSL,Isoa_OPSH,laser_power_sp
 ---laser_power_target  2.5
 ---laser_power_initial 2.45000004768372
 ---Isoa_OPSL_initial   2.67
 ---Isoa_OPSH_initial   2.67
 ---laser_power_sp_initial  2.67
 ---Isoa_OPSL   2.67
 ---Isoa_OPSH   2.67
 ---laser_power_sp  2.67
 ---laser_pwr_tune  2.45000004768372
 ---laser_power_target056   2.46
 ---SOA_MinCurrent_EDATA    23.7778471526403
 ---SOA_MinCurrent_Chan 22
 ---SOA_MaxCurrent_EDATA    45.1157734448841
 ---SOA_MaxCurrent_Chan 98
 ---SOA_Current_initial[056]    57.082
 ---SOA_Current_initial[022]    49.389
 ---SOA_Current_initial[098]    49.389
 ---laser_power 2.46
 ---SOA_Current056  57.082
 ---SOA_Current022  49.389
 ---SOA_Current098  68.437
 ---tune_rf_sp_data:tx_laser_pwr_tune_Params    Isoa_OPSL,Isoa_OPSH,laser_power_sp
 ---tune_rf_sp_data:laser_power_target  2.5
 ---tune_rf_sp_data:laser_power_initial 2.45000004768372
 ---tune_rf_sp_data:Isoa_OPSL_initial   2.67
 ---tune_rf_sp_data:Isoa_OPSH_initial   2.67
 ---tune_rf_sp_data:laser_power_sp_initial  2.67
 ---tune_rf_sp_data:Isoa_OPSL   2.67
 ---tune_rf_sp_data:Isoa_OPSH   2.67
 ---tune_rf_sp_data:laser_power_sp  2.67
 ---tune_rf_sp_data:laser_pwr_tune  2.45000004768372
 ---tune_rf_sp_data:laser_power_target056   2.46
 ---tune_rf_sp_data:SOA_MinCurrent_EDATA    23.7778471526403
 ---tune_rf_sp_data:SOA_MinCurrent_Chan 22
 ---tune_rf_sp_data:SOA_MaxCurrent_EDATA    45.1157734448841
 ---tune_rf_sp_data:SOA_MaxCurrent_Chan 98
 ---tune_rf_sp_data:SOA_Current_initial[056]    57.082
 ---tune_rf_sp_data:SOA_Current_initial[022]    49.389
 ---tune_rf_sp_data:SOA_Current_initial[098]    49.389
 ---tune_rf_sp_data:laser_power 2.46
 ---tune_rf_sp_data:SOA_Current056  57.082
 ---tune_rf_sp_data:SOA_Current022  49.389
 ---tune_rf_sp_data:SOA_Current098  68.437

1 个答案:

答案 0 :(得分:0)

如果您提供更多我可以帮助您的信息,我在这里有点困惑。

你有多个文件,有些文件用空格分割每个列的值,文件中没有任何分号,而其他文件用分号拆分每列的值,文件里面没有空格正确吗?

如果上一个问题为真且文件中的每一行都包含将填充每一行的信息,那么我将使用:

Dim r as StreamReader as New StreamReader(FilePath)
Dim Line as String

Do While StreamReader.Peek() > -1 //Go through the file until EOF

Line = r.readline() //Read the line and put it into Line
If InStr(Line, " ") Then  //Search for spaces inside Line
//This file is separated by spaces, put the code to add to the table.
Else If InStr(Line, ";") Then //search for semicolons inside line.
//This file is separated by semicolons, put the code to add to the table.
End If

Loop

我希望这会有所帮助。如果没有,请向我们提供更多细节。