[Webinar] Streamline your web hosting managementRegister Today


Retrieving unknown string from File

Posted on 1999-11-16
Medium Priority
Last Modified: 2010-05-02
  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.
Question by:itailevitan
LVL 43

Accepted Solution

TimCottee earned 160 total points
ID: 2211578
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.

LVL 13

Expert Comment

ID: 2211597
Shouldn't you always use FreeFile when setting file numbers?
LVL 43

Expert Comment

ID: 2211674
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Expert Comment

ID: 2213776
>>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. :)

Author Comment

ID: 2214402
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.

LVL 43

Expert Comment

ID: 2214416
No rush, studies are usually far more important anyway!

Author Comment

ID: 2223031
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...

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question