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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read the content of text file?

Status
Not open for further replies.

baltog

Programmer
Dec 19, 2002
22
US
Hi,

does anyone can give me sample codes how to read the content of text file?

sample:

Content of Test.txt
***************
Name: Aztec
Surname: Ocfemia
**************

I want to get the the Name: Aztec and store it in a vb variable.

Thanks in advance.
Rodel

 
Create a new form, place a command button and a text box on the form, This code will read test.txt located in the root of c. The variable is TextFile, and you can use it to place the test.txt contents anywhere, in this case into Text1. In other words use "whatever = TextFile"

ReadTextFile = "C:\01.txt"
Open ReadTextFile For Input As #1
Input #1, TextFile
DoEvents
Text1.Text = TextFile
Close #1 "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Sorry here is the right code to place into Command1:

ReadTextFile = "C:\test.txt"
Open ReadTextFile For Input As #1
Input #1, TextFile
DoEvents
Text1.Text = TextFile
Close #1 "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
First goto:
Project --> References
then select Microsoft Scripting Runtime

Then in your code add the following
[sub]
Dim objFso as new FileSystemObject
Dim objTxtStr as TextStream
Dim strFileContent as String

set objTxtStr = objFso_OpenTextFile(your filename)
Do while not (objTxtStr.AtEndOfStream)
strFileContent = strFileContent & objTxtStr.ReadLine
Loop
[/sub]

Now the variable strFileContent contains everthing from your textfile, so you'll only have to parse this string to retrieve the name I love work.
I can just sit and look at it for hours.
 
Hi Vec

Thanks 4 your great codes. But in my case, Namlop's codes would be much more applicable.

Thanks Namlop.
 
baltog, yeah I noticed that mine will onl;y read the first line where his reads it all. I was just taking a quick stab at it. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Code:
Public Function basGrabFile(FilIn As String) As String

    'Michael Red    3/3/2003
    'Sample Usage:  ? basGrabFile("C:\MsAccess\DrawArcsInVB.Txt")
    'Note the Arg [FilIn] is the FULLY QUALIFIED PATH of the Source _
     and the entire text is returned to the caller [prog | procedure]

    Dim MyFil As Integer

    Dim MyTxt As String
    Dim MyPrts() As String
    Dim MyPrtRec() As String

    'Just grab the Stuff
    MyFil = FreeFile

    Open FilIn For Binary As #MyFil

    MyTxt = String(LOF(MyFil), " ")
    Get #MyFil, 1, MyTxt

    Close #MyFil

    basGrabFile = MyTxt

End Function

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top