al4629740
asked on
subscript out of range error - looping error
I have a folder on my computer where log files are kept for internet browsing. The naming convention for those files are in a date format. (attached)
When I select a date range from my vb6 (DTPicker1,DTPicker2) form, the code below searches through those files looking for any lines containing the string "BlockedIP". When I select a date range of only one day, the code below works fine. However when I select more than one day such as 112314 to 112414, then I get the error "subscript out of range" on line 34.
What issue am I facing with the loop? Any ideas on resolving?
Thanks Much
112314.txt
When I select a date range from my vb6 (DTPicker1,DTPicker2) form, the code below searches through those files looking for any lines containing the string "BlockedIP". When I select a date range of only one day, the code below works fine. However when I select more than one day such as 112314 to 112414, then I get the error "subscript out of range" on line 34.
What issue am I facing with the loop? Any ideas on resolving?
Thanks Much
Private Sub Command2_Click()
Dim iFiles As Long, sFirstFile$, sLastFile$, sFile$
Dim f As Integer
Dim strLine As String
Dim lngLines As Long
Dim arrKeys() As String
Dim bFound As Boolean
Dim bFirst As Boolean
Dim intCount As Integer
Dim strParts() As String
Dim intTot As Integer
bFirst = True
f = FreeFile
sFirstFile = Format(DTPicker1.Value, "yymmdd") 'May 22, 2014 = 140522
sLastFile = Format(DTPicker2.Value, "yymmdd") 'June 15, 2014 = 140615
For iFiles = Val(sFirstFile) To Val(sLastFile) 'loop will proceed if starting number <= ending number
' this is required since we're reading the YY first...
sFile = Right(iFiles, 4) & Left(iFiles, 2) 'take the YY and put it at the end as in 140522 to 052214
'// now construct the current filename FQN
sFile = "C:\users\Alpesh\Desktop\test\" & sFile & ".txt"
'// skip to the next file loop if current file does not exist
If (Dir(sFile) <> "") Then
Open sFile For Input As #f
ReDim arrKeys(1, 0)
Do Until EOF(f)
Line Input #f, strLine
bFound = False
If InStr(strLine, "BlockedIP") > 0 Then
intTot = intTot + 1
strParts = Split(strLine, "keyword")
For lngLines = 0 To intCount - 1
If arrKeys(1, lngLines) = strParts(1) Then
arrKeys(0, intCount - 1) = arrKeys(0, intCount - 1) + 1
bFound = True
Exit For
End If
Next
If Not bFound Then
If Not bFirst Then
ReDim Preserve arrKeys(1, intCount)
End If
arrKeys(1, intCount) = strParts(1)
arrKeys(0, intCount) = 1
bFirst = False
intCount = intCount + 1
End If
End If
Loop
Close
End If
Next iFiles
Dim intIW As Integer
For lngLines = 0 To intCount - 1
Debug.Print arrKeys(0, lngLines) & "-" & arrKeys(1, lngLines)
intIW = intIW + arrKeys(0, lngLines)
Next
Debug.Print "Total number found " & intTot
Debug.Print "Total reported "; intIW
End Sub
112414.txt112314.txt
the "strParts" variable is declared as a zero-based, one-dimensional array on line 10, and line 32
Dim strParts() As String
strParts = Split(....) <<-- this function returns a zero-based array
so on line 34, the subscript should be zero, not one.
If arrKeys(1, lngLines) = strParts(0) Then
Dim strParts() As String
strParts = Split(....) <<-- this function returns a zero-based array
so on line 34, the subscript should be zero, not one.
If arrKeys(1, lngLines) = strParts(0) Then
Because you are using integers to control your loop, your program can only process 32,767 lines from your logs. Because of that small size limit, I never use integers. I always use "long" data types, which have a maximum value of 2,147,483,647.
ASKER
Dany,
Your solution seems to work best.
The output is (which is correct)
Total number found 44
Total reported 0
However, it no longer outputs the actual sites that were blocked. For example, it would output all the 44 sites that were found and how many times each site was blocked. See below. Any ideas why resetting the variables is causing that?
Total number found 44
Total reported 0
7- adnxs.com: ib.adnxs.com. -> 192.168.1.100
1207- blogspot.com: adsense.blogspot.com. -> 192.168.1.100
20- visualwebsiteoptimizer.com : dev.visualwebsiteoptimizer .com. -> 192.168.1.100
41- ^.*s(3|e)x: expertsexchange.112.2o7.ne t. -> 192.168.1.100
389- ^.*\.(asp|aspx|htm|html|js p|php|xml) -: www.xml-sitemaps.com. -> 192.168.1.100
1- adsrvr.org: match.adsrvr.org. -> 192.168.1.100
etc.....
Your solution seems to work best.
The output is (which is correct)
Total number found 44
Total reported 0
However, it no longer outputs the actual sites that were blocked. For example, it would output all the 44 sites that were found and how many times each site was blocked. See below. Any ideas why resetting the variables is causing that?
Total number found 44
Total reported 0
7- adnxs.com: ib.adnxs.com. -> 192.168.1.100
1207- blogspot.com: adsense.blogspot.com. -> 192.168.1.100
20- visualwebsiteoptimizer.com
41- ^.*s(3|e)x: expertsexchange.112.2o7.ne
389- ^.*\.(asp|aspx|htm|html|js
1- adsrvr.org: match.adsrvr.org. -> 192.168.1.100
etc.....
The "intIW" variable is not being incremented in the loop because the loop didn't run, thus the zero result.
So the For...Next loop on line 58 should reference the intTot variable instead of intCount.
Change:
To:
So the For...Next loop on line 58 should reference the intTot variable instead of intCount.
Change:
For lngLines = 0 To intCount - 1
To:
For lngLines = 0 To intTot - 1
ASKER
After changing it to
For lngLines = 0 To intTot - 1
I get subscript out of range error on line 59. Do I need to reset the variables also in this case before entering the loop?
For lngLines = 0 To intTot - 1
I get subscript out of range error on line 59. Do I need to reset the variables also in this case before entering the loop?
Can u try my code plz? I tested it in vb6 it works
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dany, YOU GOT IT!!!!!
Glad i could help :)
you are forgetting to reset your variables at the beginning of the loop (these variables are important to redim your array)
just add these and your code will work:
bFound = False
bFirst = True
intCount = 0
Open in new window