Error on VB Script Type mismatch

Hi,
I´m working with the following script, it is working ok, but on some logs, I get an error that says:
Microsoft VBScript runtime error: Type mismatch: 'PreviousFinds'
How can I fix this error?
strFileName = Wscript.Arguments.Named("file") 'Get file name from argument
If strFileName = "" Then
    Wscript.Echo "Message: You must specify a File Name like this: /file:""blah.txt"""
    Wscript.Quit(4)
End If

strSearch = Wscript.Arguments.Named("search") 'Get search string form argument
If strSearch = "" Then
    Wscript.Echo "Message: You must specify a string to search for like this: /search:""ERROR"""
    Wscript.Quit(4)
End If

If strFailIf="" Then strFailIf="found" End If
If (strFailIf="found") Then
	strFound=3
	strNotFound=0
ElseIf (strFailIf="notfound" or strFailIf="not found") Then
	strFound=0
	strNotFound=3
Else
	strFound=4
	strNotFound=4
	Wscript.Echo "Message: ERROR, set strFailIf"
    Wscript.Quit(4)
End If	

yyyy = Datepart("yyyy",Date())
yy = Right(yyyy, 2)
m = Datepart("m",Date())
mm = Right("0" & m, 2)
d = Datepart("d",Date())
dd = Right("0" & d, 2)

If (InStr(1,strFileName,"${Date}",1) <> 0) Then
	If (InStr(1,strFileName,"${Date}",1) <> 0 AND strDateFormat <> "") Then
		strDateFormat = replace(strDateFormat, Space(1), " & Space(1) & ")
		strDateFormat = replace(strDateFormat, "_", " & ""_"" & ")
		strDateFormat = replace(strDateFormat, "/", " & ""/"" & ")
		strDateFormat = replace(strDateFormat, "-", " & ""-"" & ")
		strDateFormat = replace(strDateFormat, ".", " & ""."" & ")
		strFileName = Replace(strFileName,"${Date}",eval(strDateFormat),1,-1,1)
	Else 
		Wscript.Echo "Message: Error with ${Date} and strDateFormat. Please review instructions at top of script."
		Wscript.Quit(4)
	End If
End If

strPattern = strSearch

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists("temp") Then
	objFSO.CreateFolder("temp")
End If

PrevLogFile =  "temp\" & strSearch & "-" & mm & "_"  & dd-1 & "_"  & yyyy & ".txt"
If objFSO.FileExists(PrevLogFile) Then
	set objPrevLogFile = objFSO.GetFile(PrevLogFile)
	objPrevLogFile.Delete
End If

PreviousFinds = 0
LogFile = "temp\" & strSearch & "-" & mm & "_"  & dd & "_"  & yyyy & ".txt"
If objFSO.FileExists(LogFile) Then
	Set objLog = objFSO.OpenTextFile(LogFile,1, True)

	If (IsEmpty(objLog) = True ) Then PreviousFinds = 0 End If
PreviousFinds = objLog.ReadAll
End If

If objFSO.FileExists( strFileName ) Then
  Set objFile = objFSO.GetFile(strFileName)
  If( IsEmpty( objFile ) = True ) Then
      WScript.Echo "Message: OBJECT NOT INITIALIZED"
      WScript.Echo "Statistic: 0"
      WScript.Quit(4)
    End If
     
  else
    WScript.Echo "Message: The file """ & strFileName & """ was not found!<BR>This could indicate that the file does not exist or you do not have the correct credentials for monitoring. <BR>" 
    WScript.Echo "Statistic: 0"
    WScript.Quit(strNotFound)
End If

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strPattern
objRegEx.IgnoreCase = True
Set objReadFile = objFSO.OpenTextFile(strFileName, ForReading)
x=1
Do Until objReadFile.AtEndOfStream
    strSearchString = objReadFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
			If int(x) > int(PreviousFinds) Then		
				Wscript.Echo "Statistic: " & PreviousFinds+1 
				Wscript.Echo "Message: New search for """ & strSearch & """ was found: " & strSearchString
					Set objLog = objFSO.OpenTextFile(LogFile, 2, True)
					objLog.WriteLine(Now & " - "  & PreviousFinds+1)
					objLog.Close
				Wscript.Quit(StrFound) 
			End If
		x=x+1
        Next
    End If
Loop
If int(x) >1 Then
	Wscript.Echo "Statistic: " & PreviousFinds
    Wscript.Echo "Message: New search for """ & strSearch & """ was found, but previous searches did find """ & strSearchString & """."
    Wscript.Quit(StrFound) 	
End If
Wscript.Echo "Statistic: " & PreviousFinds 
Wscript.Echo "Message: Search for """ & strSearch & """ was not found."
Wscript.Quit(strNotFound)
objReadFile.Close

Open in new window

andresskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
The error is in lines 69 and 70, in the case of an empty log file.
You check whether the log file is empty, but ReadAll is executed even if it is, resulting in PreviousFinds being set to an empty string. The int(PreviousFinds) in line 97 then throws the error.
Just extend the if clause starting in line 69:
If objFSO.FileExists(LogFile) Then
	Set objLog = objFSO.OpenTextFile(LogFile,1, True)
	If (IsEmpty(objLog) = True ) Then
		PreviousFinds = 0
	Else
		PreviousFinds = objLog.ReadAll
	End If
End If

Open in new window

andresskAuthor Commented:
Hi oBdA,
I already have made the suggested change but I keep on getting the same error.

Thanks
oBdACommented:
Sorry, spoke too soon. Had a closer look, and it's not quite clear what you're doing/expecting in the "LogFile".
You're trying to read it in its entire length in line 70, and trying to convert this to a numeric value in line 97 (in other words: the script throws the error as soon as "LogFile" contains non-numeric characters), which seems to indicate that you expect "LogFile" to just contain a number.
But then again, you're changing the access to LogFile to write (without closing it first, btw) and are then writing a (non-numeric) date into the file.
So what are you trying to do here?

Oh, and what I missed in the first comment: if you actually want to check for an empty file, you need to do something like the example below, "IsEmpty()" won't work because you've established that the file exists, so the objFSO.OpenTextFile() should return an object.
If objFSO.FileExists(LogFile) Then
	If objFSO.GetFile(LogFile).Size = 0 Then
		Wscript.Echo "Logfile has no content"
		PreviousFinds = 0
	Else
		Set objLog = objFSO.OpenTextFile(LogFile, 1, True)
		PreviousFinds = objLog.ReadAll
		If Not IsNumeric(PreviousFinds) Then
			Wscript.Echo "Contents in logfile not numeric: " & PreviousFinds
			PreviousFinds = 0
		End If
	End if
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

andresskAuthor Commented:
Hi oBdA,
What I´m trying to check is that if the script finds a string text, for example the word "error", and on every word that it finds, it shows an statistic to count the number of found errors in a period of time, changing the string into a numeric value to get a statistic.

I´m testing with different logs at the moment to check the performance.

Thanks
andresskAuthor Commented:
Hi,
Checking the search for error results since last 24 hours, I still get false positives because the script search for an error, but the error that it reports is old. Is ther any way to make a script to search for the error string and also compares the date and time to send an alarm?

Thanks you
andresskAuthor Commented:
The error that I´m getting right now is:
 "Microsoft VBScript runtime error: Bad file name or number"
andresskAuthor Commented:
Found the problem and it´s that the search string was too long. Now I need another check. I´ll open another post for it.
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.