Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About working with text files 1

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hi

It's been a while since I posted a question as, thanks in no small part to you lot here, I've got better at sussing out any problems for myself.

But still, I have a query.

If I open a text file with e.g.
Code:
Set objAccountsOnlyList = objFileSystem.OpenTextFile(strAccountsList, ForReading)
then search for text within it (by matching a variable against an array element taken from the text file) with
Code:
    text = objAccountsOnlyList.ReadAll
    arrText = Split(text, vbCrLf)
    For Each elem In arrText
        If elem = "" then
        ' do Nothing, it's just a blank line that we don't care about.
        Else
            strAccount = Split(elem,",",2)
            If LCase(strAccount(0)) = LCase(strSplitUserName) Then
                strUsersFullName = strAccount(1)
                wscript.echo "Found " & LCase(strAccount(0)) & " (" & strUsersFullName & ")"
                bUserNameFound = True
                Exit For
            End If
        'objStrFile2.write elem
        End If
    Next
do I have to close and reopen that text file should I need to search through it again?

I ask because I've set up some recursive code but on the second pass I get this error:
Code:
Microsoft VBScript runtime error: Input past end of file
I've commented out
Code:
objAccountsOnlyList.Close
and the
Code:
Set objAccountsOnlyList = Nothing
line hasn't been called when the error occurs so I can't blame them.

Any suggestions?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
the contents of the text file should now be available to you whenever you want, you have put it in

text = objAccountsOnlyList.ReadAll

so, just reuse 'text'...i presume it will stay resident even after you do a .close (byval or ref...)

i would advise that you check UBound(strAccount) > 1 before your strAccount(0 or 1) as you might have a non blank line which doesnt have enough , in it
 
Well the error is being created by
Code:
text = objAccountsOnlyList.ReadAll
so presumably I need to re-arrange my code to parse this line only the once?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
well you could have function which reads the contexts of a file into the 'text' variable. once this variable has been populated with the data then you can off and pass that to another sub which does the iterations of the contents of the file which is in memory.

this means you

1. read the contents of the file once into your own variable
2. reuse this variable as many times as you want, hence only opening the file once not x times

which i guess was your goal anyway :)

either way you shoudl always .close then set nothing after you have finished reading the file and always try and just read the file once...
 
Precisely.

Well, I've moved the two lines that read in the text file and create the array and now my recursive code is behaving.

Well, almost, but the problem resulting from this thread has been resolved thanks to you.

Thanks, MrMovie!

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top