在文本文件中搜索并执行

时间:2013-07-21 13:09:56

标签: vb.net search text

我真的是VB的初学者,所以如果你能帮助我,我会感激不尽。我的问题非常简单。我有一个简单的.txt文件(比如说test.txt)我想在两个特定字符(比如字符5和6)上搜索特定行(比如第22行)。如果找到的数字大于18,则执行.bat文件。如果不做什么。

我很感激一些帮助!

1 个答案:

答案 0 :(得分:0)

这应该为您提供一些您需要的示例:

    Public Sub FindCode()

    Dim tempLines As List(Of String)
    Dim position As Int32


    tempLines = ReadFileLines("c:\filename.txt")

    If tempLines Is Nothing Then
        MessageBox.Show("file not read")
        Return
    End If

    If tempLines.Count < 22 Then
        MessageBox.Show("Line 22 does not exist")
        Return
    End If

    'position of line is one less than we are lookign for since List(Of T) is zero based
    position = 22 - 1

    If tempLines(position).Length < 6 Then
        MessageBox.Show("Line 22 does not have 6 characters")
        Return
    End If

    If IsNumeric(tempLines(position).Substring(4, 2)) = False Then
        'characters 5 and 6 (zero based (4) for 2 characters)
        MessageBox.Show("characters 5-6 are not numeric")
    End If

    If CInt(tempLines(position).Substring(4, 2)) <= 18 Then
        MessageBox.Show("characters 5-6 are less than or equal to 18")
        Return
    End If

    System.Diagnostics.Process.Start("c:\batchfile.bat")


End Sub

Public Function ReadFileLines(p_fileName As String) As List(Of String)
    If System.IO.File.Exists(p_fileName) = False Then
        MessageBox.Show("File does not exist")
        Return Nothing
    End If

    Return System.IO.File.ReadAllLines(p_fileName).ToList()

End Function