Retrieving unknown string from File

  I would like to know how to write the code for:
1) Access a certain file.
2) Open it.
3) Search for a certain string A in it.
4) skip 1 newline character.
5) Retrieve a string B that ends in the next newline character (not including).
6) Close file.
7) I am new to VB so please make it clear and very simple. Thank-you.
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.

TimCotteeHead of Software ServicesCommented:
Dim strLine As String
Dim strLine2 As String

Open "AnyFile.txt" For Input AS #1
  Input #1,strLine
  IF Instr(1,strLine,"StringA") > 0 Then
    Input #1,strLine2
    Exit Do
  End If
Loop Until Eof(1)
Close #1

This opens the file, reads it line by line until it finds an occurrence of "StringA" returned, it then reads the next line and exits the loop, closes the file and returns the second string in a message box.


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
Shouldn't you always use FreeFile when setting file numbers?
TimCotteeHead of Software ServicesCommented:
Sure you should use freefile in practice, I just knocked this off the top so didn't!

But to help itailevitan,

before the open statement add the line,

FileNo = FreeFile

And replace all occurrences of #1 with #FileNo.

It makes no real difference if you are only ever opening one file at a time and you are sure that the file is closed before trying to open another one (or the same one again).
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

>>Shouldn't you always use FreeFile when setting file numbers?

FreeFile must be used when you're doing a large project, or a routine which will be used in several different contexts, where you don't know if now channel #24 is open or not.

With fool or very tiny things, the use of FreeFile has no reason. But, of course, it helps to good programming habit. :)
itailevitanAuthor Commented:
Hello Tim,

    At the moment I haven't got the time to check your answer - I will tommorow (I am a very busy working student from Israel). I will check it tommorow -so you won't have to wait.
Thank you very much for now! Itai.

TimCotteeHead of Software ServicesCommented:
No rush, studies are usually far more important anyway!
itailevitanAuthor Commented:
Hello Tim,
Thanks for the code but there is a slight problem:
- I am trying to read a "user_id" from a certain cookie.
- I try to read a new line using your code - but it reads the whole file content into the string! (although if I open the cookie with Word - I see that there are new lines, so there must be new line characters in the cookie file, no?)
- As a result: I crash when trying to execute the command: Input #1, strLine2
(according to your code -see above)
- If I can find out how to advance the file pointer, that would help as well.
- To sum up: I need to skip the newline character(that I saw with Word) which appears after the string: user_id that I am looking for and then read a string (representing the numeric id) which ends in the next newline character(this, again, I saw with Word)
- Please tell me what to do - 10'x
P.S. I miss C/C++ - this would have been over a long time ago...
although VB has its features...
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
Visual Basic Classic

From novice to tech pro — start learning today.