2017-10-11 45 views
1

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

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

也许,不是为单词使用字符串数据类型,而是尝试使用一个(字符串)列表,并使用循环来处理每个分号分割,将它们添加到列表中,并循环遍历数据列表表格行 – AustinS90

回答

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 

我希望这有助于。如果没有,请给我们提供一些细节。

+0

嗨,Alvaro,我已经添加了详细信息。 – Badz

+0

嗨,Alvaro,我已经通过在button_click上放置了数据表来解决了这个问题。现在完美地工作。谢谢你的时间。 – Badz

相关问题