Hi all

I have two parts of code; one is searching (InStr) if log file contains certain string and the other one find the last modified log in one folder.
Now I would like to merge these two together and modify so the script finds the last modified log file from all the logs in folder that contain certain string.

These are the two code snipets:
1. finds a string in log file:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("C:\folder\")
						
for each file in folder.Files
if lcase(objFSO.getExtensionName(file.path))="log" then 
Set myFile = objFSO.OpenTextFile(file.path, ForReading)
Do While Not myFile.AtEndOfStream
myLine = myFile.ReadLinemyLine = myFile.ReadLine

If InStr(myLine, "string 2 search for") Then 
'action 1
else
'action 2
End If 								 
Loop
myFile.close
end if 
next

2. Finds the last modified log in logs folder:

Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set FolderToScan = objFSO.GetFolder("C:\folder\")
		dim filesys, text, readfile, contents, FilePath

		Set objFolder = objFSO.GetFolder(FolderToScan)

		NewestFile = ""
		NewestDate = #1/1/1970#

		For Each objFile In objFolder.Files
					
		if lcase(objFSO.getExtensionName(objFile.path))="log" then	
					
			If objFile.DateLastModified > NewestDate Then
			NewestDate = objFile.DateLastModified
			NewestFile = objFile.Name
			FilePath = objFile.Path
							
			End If
		end if
		next
		WScript.Echo NewestFile

Any help would be appreciated.
tnx

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.

'
'  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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.