First, some pseudo code to get the logic.
'find the most recently modified log file containing a given string
mystring = "ERROR"
newestfile = ""
newestdate = someolddate
for each file in the folder
if this is a log file
if the file contains mystring
if this file is newer than newestdate
newestfile = file_name
newestdate = file_modified_date
end if
end if
end if
next
if newestfile <> ""
msgbox("the most recently modified log file containing " & mystring & " is " & newestfile)
else
msgbox("no log file found containing " & mystring)
end if
The easiest way to get a list of files in a folder is to use native VB objects. Iin this case
For Each file As String in My.Computer.FileSystem.GetFiles(foldername)
And because you only want log files, you can filter on those by
For Each file As String In My.Computer.FileSystem.GetFiles(foldername, FileIO.SearchOption.SearchTopLevelOnly, "*.log")
An obvious candidate for a separate function is the code that scans a file for a string. You could name is something like FileContains, pass it the file name and the string and have it return True or False. I'll post more in a few minutes.
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
'
' Name:
'
' ScanLogs
'
' Description:
'
' Displays the most recently modified log file in the given folder that contains
' a specified string.
'
' Audit:
'
' 2012-02-13 name - original code
'
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myString As String = "Splunge" 'string to search for
Dim myFolder As String = "d:\temp" 'folder containing log files to scan
Dim newestfile As String = "" 'name of most recently modified log file
Dim newestdate As Date = CDate("january 1 1900") 'date of most recently modified log file
'scan a folder for all log files
For Each file As String In My.Computer.FileSystem.GetFiles(myFolder, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
'scan file for string (False = case insensitive)
If FileContains(file, myString, False) Then
Dim lastmoddate As Date = My.Computer.FileSystem.GetFileInfo(file).LastWriteTime
'if newer log file then save file name and mod date
If lastmoddate > newestdate Then
newestfile = file
newestdate = lastmoddate
End If
End If
Next
If newestfile <> "" Then
txtQuery.Text = newestfile
Else
txtQuery.Text = "not found"
End If
End Sub
Function FileContains(filename As String, searchTerm As String, caseSensitive As Boolean) As Boolean
'returns True if the given file contains the given string
Dim text As String = My.Computer.FileSystem.ReadAllText(filename)
If caseSensitive Then
Return InStr(text, searchTerm, Microsoft.VisualBasic.CompareMethod.Binary) > 0
Else
Return InStr(text, searchTerm, Microsoft.VisualBasic.CompareMethod.Text) > 0
End If
End Function
End Class
It's always worth noting that a browse through available methods can turn up some interesting time savers. For example, there is a FindInFiles method that does almost all the work for you resulting in the following short code
'scan a folder for all log files containing the given string
For Each file In My.Computer.FileSystem.FindInFiles(myFolder, myString, True, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
Dim lastmoddate As Date = My.Computer.FileSystem.GetFileInfo(file).LastWriteTime
'if newer log file then save file name and mod date
If lastmoddate > newestdate Then
newestfile = file
newestdate = lastmoddate
End If
Next
Generally speaking, it is often faster to let the system do what it does.
Please also note the use of a header, comments and white space.
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159