Solved

Search for a log based on date

Posted on 2008-10-13
14
179 Views
Last Modified: 2012-05-05
I need to search for a log based on the date and then look for a pattern within that log. For example on Oct 13th, I need to look for:
log.2008-10-13

How can i do that in vbscript?
0
Comment
Question by:rheasam
  • 6
  • 4
  • 4
14 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 22707155
Hi, something like this might do the job....

Regards,

Rob.
strFolder = "C:\Logs"

Set objFSO = CreateObject("Scripting.FileSystemObject")

strYear = Year(Date)

strMonth = Right("0" & Month(Date), 2)

strDay = Right("0" & Day(Date), 2)

strLogName = "log." & strYear & "-" & strMonth & "-" & strDay

strFoundFile = ""

For Each objFile In objFSO.GetFolder(strFolder).Files

	If LCase(objFile.Name) = LCase(strLogName) Then strFoundFile = objFile.Path

Next

If strFoundFile = "" Then

	MsgBox strLogName & " was not found in " & strFolder

Else

	MsgBox "Found " & strFoundFile

End If

Open in new window

0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 22715760
* There's probably a more direct way of doing this using FileExists. see snippet
* What pattern do you need to find inside the log file?
strFolder = "C:\Logs\"

Set objFSO = CreateObject("Scripting.FileSystemObject")

strLogName = "log." & Format(Now, "yyyy-mm-dd")

If objFSO.FileExists(strFolder & strLogName) Then

   MsgBox "Found " & strFolder & strLogName

   '? look for pattern in the file ?

Else

   MsgBox strLogName & " was not found in " & strFolder

End If

 

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22715898
>> There's probably a more direct way of doing this using FileExists

Oh, of course.....brain fart moment.....  :-(

That's true aikimark, but the Format method does not exist in VBScript....

Rob.
strFolder = "C:\Logs"

Set objFSO = CreateObject("Scripting.FileSystemObject")

strYear = Year(Date)

strMonth = Right("0" & Month(Date), 2)

strDay = Right("0" & Day(Date), 2)

strLogName = "log." & strYear & "-" & strMonth & "-" & strDay

If objFSO.FileExists(strLogName) Then

	MsgBox strLogName & " was not found in " & strFolder

Else

	MsgBox "Found " & strLogName

End If

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 22716780
I, too, forgot about the watered-down nature of the VBScript run time environment.  (no Format function)
0
 

Author Comment

by:rheasam
ID: 22721431
Ok, thanks I am checking on these and will let you know.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22722206
There are a few work-arounds for VBScript folks who find they need to use the Format() function often.  The simplest is to use VB-classic to create an ActiveX DLL that returns the result of the VB Format() function.  Then it is a simple matter of instantiating a variable of that type and use its one and only  method.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22726695
Or you can write your own formatting function in code, something like the below....

It's obviously not very "smart", but it could be made to output in multiple formats....

Regards,

Rob.
MsgBox TimeNow(Now)
 

strDate = Left(TimeNow(Now), 4) & Mid(TimeNow(Now), 9, 2) & Mid(TimeNow(Now), 6, 2)
 

MsgBox strDate
 

Function TimeNow(dDateTime)

	TimeNow =	Year(Now) & "-" &_

				Right("00" & Month(Now), 2) & "-" &_

				Right("00" & Day(Now), 2) & "-" &_

				Right("00" & Hour(Now), 2) & "-" &_

				Right("00" & Minute(Now), 2) & "-" &_

				Right("00" & Second(Now), 2)

End Function

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rheasam
ID: 22868270
Can you give me the code to look for the particular pattern, the above seems to just check whether the log with date is present.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22868363
You could construct a pattern matching string to use with the LIKE operator
For Each objFile In objFSO.GetFolder(strFolder).Files

 If LCase(objFile.Name) Like "Log.2008-10-13*" Then strFoundFile = objFile.Path

Next

Open in new window

0
 

Author Comment

by:rheasam
ID: 22868522
No what I meant is to look for a pattern within the file. So for example I found the file
Log.2008-10-13. Now I need to look for badfetch string within that file:

ex:
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22868745
@rheasam

You haven't given us any information about the contents or size or format of the file.  I could give you an answer, but I'd rather give you a good answer.
0
 

Author Comment

by:rheasam
ID: 22868822
The file is a text file and will always have some content. The lines could like the following


Could not read the file: badfetch error  12:25 13 Oct 2008
Could not retrieve the audio:badtfetch error 12:26 13 Oct 2008


Whenever the badfetch comes I want to print the line and send it as a alert. I will generate a email with that text.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22868960

Dim strLine As String

Open "filename" For Input As #1

Open "alertfilename" For Output As #2

Do Until EOF(#1)

  Line Input #1, strLine

  If strLine Like "*badfetch*" Then

    Print #2, strLine

  End If

Loop

Close

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22872003
The above is VBA code, try this for VBScript.

Regards,

Rob.
strFolder = "C:\Logs"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Const intForReading = 1

strYear = Year(Date)

strMonth = Right("0" & Month(Date), 2)

strDay = Right("0" & Day(Date), 2)

strLogName = "log." & strYear & "-" & strMonth & "-" & strDay

If objFSO.FileExists(strLogName) Then

	MsgBox strLogName & " was not found in " & strFolder

Else

	MsgBox "Found " & strLogName

	Set objFile = objFSO.OpenTextFile(strLogName, intForReading, False)

	While Not objFile.AtEndOfStream

		strLine objFile.ReadLine

		If InStr(strLine, "badfetch") > 0 Then

			WScript.Echo strLine

		End If

	Wend

	objFile.Close

	Set objFile = Nothing

End If

MsgBox "Script has finished."

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now