Hello! I'm currently running in to an issue where I need to loop through a specific log file until a string of text occurs, which indiciates that I can continue on with the application. Here is my code and logic:

    Dim process As New Process()
    Dim serverLog As String
    Dim instance As String
    Dim server As String
    Dim list As New ArrayList()

    server = cmdTextbox1.Text
    instance = cmdInstance.Text

    Dim rdsLog As String = "\\" & server & "\c$\rds\server\" & instance & "\log\server.log"

    Dim i As Integer
    Dim ItemList As New ArrayList()

    process.StartInfo.UseShellExecute = False
    process.StartInfo.RedirectStandardOutput = True
    process.StartInfo.RedirectStandardError = True
    process.StartInfo.CreateNoWindow = True
    process.StartInfo.FileName = FileName
    process.StartInfo.Arguments = Arguments
    Dim output As String = process.StandardOutput.ReadToEnd()

    serverLog = output
    Dim resultQuery As String
    Dim query As String = "select count(*) from " & rdsLog & " where text like '%Server has been started in Online Mode%'"
    Dim parser As New LogQueryClass
    Dim rsLp As ILogRecordset
    Dim rowLp As ILogRecord

    resultQuery = 0
    Do While resultQuery = 0
        rsLp = parser.Execute(query)
        Do While Not rsLp.atEnd
            rowLp = rsLp.getRecord
        resultQuery = ItemList(i)

    If resultQuery = 0 Then
        MsgBox("Not started")
        MsgBox(" " & server & "\" & instance & " has STARTED")
    End If

This works fine if the logfile already contains the string of text, however it's not re-iterating through the recordset to check and see if that value exists. There is probably a much simpler way to accomplish this. Thank you for your time!

Is this file "live" ie are you looping through the file until something has written to the file?

In which case you will have to reload the file in each loop but ensure you only open it as read only or you will lock the file for editing and prevent it being written to.

Hey there - I've recently come back to this project and switched over to using StreamReader instead of using LogParser. Here is the code:

                    If File.Exists("\\" & server & "\x$\server\client\" & Code & "\bin\wrt.bat") Then
                        Shell("cmd /k x:\user\PsExec.exe \\" & server & " x:\server\client\" & Code & "\bin\wrt.bat start >>wrt_bat.log", AppWinStyle.Hide)
                        resultQuery = 0
                        Do Until resultQuery = 1
                            Dim sr As StreamReader = New StreamReader(path)
                            Do While sr.Peek() > -1
                                lin = sr.ReadToEnd
                                If lin.Contains(text) Then
                                    resultQuery = 1
                                    Label2.Text = server & " has started"
                                    resultQuery = 0
                                End If

However I'm getting the "file being used by another process" error. It appears the code above is causing this issue. I see I am closing out the streamreader, so I do not see what could be using the file. Please help, thanks!

This article has been dead for over six months. Start a new discussion instead.