2013-07-21 57 views
0

我真的是VB的初學者,所以如果你能幫助我,我會很感激。我的問題很簡單。我有一個普通的.txt文件(可以說是test.txt),我想在特定的行(可以說是第22行)上搜索兩個特定字符(可以是字符5和6)。如果找到的數字大於18,則執行一個.bat文件。如果不是無所事事。在文本文件中搜索並執行

我會感謝一些幫助!

回答

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

哇非常感謝!所以只是爲了理解它,如果讓我們說它不是字符5和6,它可以說字符15和16我只是把子字符串(14,2))正確嗎? – user2604223

+0

正確。如果有幫助,請記住標記爲您的答案。 –

+0

我越來越2號字符15預期結束聲明:( 代碼800A0401 – user2604223