Computing.Net > Forums > Programming > Read a txt file in Vb

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Read a txt file in Vb

Reply to Message Icon

Name: Anna
Date: September 17, 2003 at 07:00:18 Pacific
OS: win98
CPU/Ram: p-4,128mb
Comment:

Dear Friends,
How do we read text file(ASCII format) line by line in VB. After the entire file is read, then how do we delete the file.

Pls Help

Thanx

Anna



Sponsored Link
Ads by Google

Response Number 1
Name: AlwaysWillingToLearn
Date: September 17, 2003 at 08:48:04 Pacific
Reply:

Hello Anna,

this is one way in which you can read a file but not line by line.

private sub command_click()
Dim fso,f
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.opentextfile(FrmChatForum.ComOpen.FileName, ForReading)
text1.Text = f.read(1000000)
end sub

note that the set fso = createobject("Scripting.filesystemobject") should all be on one line.

NOW my fave bit to delete a file you use the kill key word eg

Kill("C:\windows\desktop\a.txt")
this will delete the a.txt file on desktop

if you want to delete a file in you current directory ie in the directory in which yoor vb code is then you may do so thus

kill(app.path & "\a.txt")

to read a file line by line you need to use the lineinput method.

If it works let me know ill try finding a way to read line by line.
Have a nice day


0

Response Number 2
Name: AlwaysWillingToLearn
Date: September 17, 2003 at 08:50:38 Pacific
Reply:

woops one thing the
FrmChatForum.ComOpen.FileName
should actually be the path of the file you are trying to read
eg:
("C:\windows\desktop\a.txt",ForReading)
Sori


0

Response Number 3
Name: AlwaysWillingToLearn
Date: September 17, 2003 at 09:27:22 Pacific
Reply:

ok ive found my line input code, the thing is the above code may be used to read the entire file into one text box line input can be used to read each line into Different text boxes.

Here how

''''CODE''''
Dim timepstring As String
Dim TheIndex As Integer

Private Sub form_load()
TheIndex = 0
End Sub

Private Sub Command1_Click()
TheIndex = 0
Open ("C:\windows\desktop\a.txt") For Input As #1
Do Until EOF(1)
Line Input #1, Tempstring
Text1(TheIndex).Text = Text1(TheIndex).Text & vbCrLf & Tempstring
TheIndex = TheIndex + 1
Loop

Close #1
End Sub

'''''END''''

alturnetively you can remove the Theindex variable to read the text into one textbox
hope the above examples help


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


ASP without session varia... excel...if or vlookup?



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Read a txt file in Vb

play a mp3 file in vb or in html www.computing.net/answers/programming/play-a-mp3-file-in-vb-or-in-html/15624.html

read a text file in assembly www.computing.net/answers/programming/read-a-text-file-in-assembly/17267.html

One line of a .txt file in VB6 www.computing.net/answers/programming/one-line-of-a-txt-file-in-vb6/8248.html