how to ensure loop reads all rows not line 1

Folk

I was able to source a loop, however this seems to only read off line 1 from a log file,see below

Do While Not objFile.AtEndOfStream
                        strData = objFile.ReadLine
                        arrInput = Split(strData,"' to '")
                        If InStr(arrInput(1),"@googlemail.com") Then objOut.WriteLine strData
                Loop

how do i have it search the entire log for the specified ),"@googlemail.com" input?

cheers
rutgermonsAsked:
Who is Participating?
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.

Robert SchuttSoftware EngineerCommented:
You are probably encountering an error on the first line of the input file. You can either check for (or ignore) errors or make sure the code that can error in a certain situation doesn't get executed in that situation, in this case having an array with only 1 element when the split text is not found in the string.

Try this:
Do While Not objFile.AtEndOfStream
                        strData = objFile.ReadLine
                        arrInput = Split(strData,"' to '")
                        If UBound(arrInput) > 0 Then
                                If InStr(arrInput(1),"@googlemail.com") Then objOut.WriteLine strData
                        End If
                Loop

Open in new window

0
Bill PrewCommented:
The loop will read all records from the file under normal circumstances.  I would need to see the rest of your code to propose changes or reasons it isn't reading it all?

Are you getting any errors?

Do you have ON ERROR RESUME NEXT in the code, which would suppress errors.

Will every line have the string " to " on it?

Can there be any blank lines in the file?

~bp
0
rutgermonsAuthor Commented:
folks

I modified roberts suggestion a bit to work for me  (added = to the greater than statememnt

                Set objFile = objFSO.OpenTextFile(objFile.Path, intForReading, False)
                'loop thorugh log file
           Do While Not objFile.AtEndOfStream
                        strData = objFile.ReadLine
                        arrInput = Split(strData, "' to '")
                        If UBound(arrInput) >= 0 Then
                                If InStr(arrInput(0), "@googlemail.com") Then objOut.WriteLine strData
                        End If
                Loop


seems to work, for me

if you see this being incorrect,would be glad for your thoughts?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Robert SchuttSoftware EngineerCommented:
Ah, but now you're checking arrInput(0), that's an important change!

Actually, Split() will always return an array of at least size 1, in that case the delimiter was not found and the first element of the array contains the entire line. If you want to check for the domain in the first part then the UBound check is not necessary.

But when you want to check the part after the delimiter (using element 1 like you posted originally) it can go wrong, as Bill and I suspected was happening.

Note that with the new code, you also include lines where the delimiter is not found, but the domain is found anywhere in the line. Gut feeling is that this is not what you want. Maybe if you describe exactly what you need (specifically the criteria that need to be met to include a line in the output), it would be possible to slightly change the code accordingly.

I guess obvious options here are to just check the entire line, or indeed go back to checking UBound > 0 because that means the delimiter was indeed found in the line.
0
rutgermonsAuthor Commented:
Rob

basically I need to search multiple log files for certain values and write the entire line to another log file, the below with your suggestion and tweaking is working the way I would like it to

strFolder = "C:\test"
Dim objFSO
Dim objFile
Dim objOut
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
 
 'create output file
 strOutputName = "output.txt"
 Set objOut = objFSO.GetFolder(strFolder).CreateTextFile(strOutputName, True)
 
'for each file in directory
For Each objFile In objFSO.GetFolder(strFolder).Files
        'if its a log file
        If Right(LCase(objFile.Name), 4) = ".log" Then
               'open log file
                Set objFile = objFSO.OpenTextFile(objFile.Path, intForReading, False)
                'loop thorugh log file
           Do While Not objFile.AtEndOfStream
                        strData = objFile.ReadLine
                        arrInput = Split(strData, "' to '")
                        If UBound(arrInput) >= 0 Then
                                If InStr(arrInput(0), "@googlemail.com") Then objOut.WriteLine strData
                        End If
                Loop

                objFile.Close
        End If
Next
objOut.Close
'MsgBox "Finished."objOut.Close
'MsgBox "Finished."
0
Robert SchuttSoftware EngineerCommented:
Ok, that part is clear but you didn't answer the question about criteria, so let me rephrase that.

The code seems ok in itself, if it's not can you say:-
- which lines are not getting copied to the output that should be copied?
- which lines are getting copied to the output that should not be copied?

Or, can you provide sample input/output? I used my own sample data but that was constructed to work with the check on arrInput(1) so now my output remains empty, but without knowing what you are trying to achieve I can't say for sure what needs to be changed (if anything).
0
rutgermonsAuthor Commented:
Rob

 the below is my test data from the log per line :

dummy

testmail@googlemail.com)dummy

dummy


note, I modified the ubound clause to "If UBound(arrInput) >= 0 " to get it to work

cheers
0
Robert SchuttSoftware EngineerCommented:
Well, that's the thing, the delimiter (' to ') is not present at all in that test data, so what are you checking/using that for?
0
Robert SchuttSoftware EngineerCommented:
The current code will not copy a line to the output if it contains the delimiter and only contains the domain in the part after the delimiter. Is that what you want?
0
rutgermonsAuthor Commented:
rob

i re-used a code i found on e-e, i am essentially only looking for the string contained in the input

"@googlemail.com"

my real requirement is to capture the string "connected" or "refused"

pardon I didnt make that clear from the onset
0
Robert SchuttSoftware EngineerCommented:
No problem, that just means you can get rid of the Split altogether. The changed code:
                Do While Not objFile.AtEndOfStream
                        strData = objFile.ReadLine
                        If InStr(strData, "connected") Or InStr(strData, "refused") Then objOut.WriteLine strData
                Loop

Open in new window

0

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
rutgermonsAuthor Commented:
cheers! even better! :)

last one, if i wanted to add a file name and timstamp
0
Robert SchuttSoftware EngineerCommented:
I'm afraid your follow-up question is not clear to me.

BTW; you've closed the question with a B grade without explanation (and indeed your next comment would seem to indicate you're happy with the solution). A lower grade is normally only used when there's obvious work to be done on your part with the provided solution and if so, it's appreciated if you explain how and why. I'm not asking you to change it because that's just not worth the hassle but if needed please review the grading guidelines for future reference.
0
rutgermonsAuthor Commented:
sorry, cancel  my last statement, i will try research that myself, if i struggle i will raise another question

in terms of grading, I stated "good" meaning i am happy with the solution, 500 points given, i will def. specify excellent next time

thanks for your help though, sincerely appreciate it
0
Robert SchuttSoftware EngineerCommented:
Ok, I understand, no worries then.
0
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.