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

Read a txt file in Vb

Reply to Message Icon

Original Message
Name: Anna
Date: September 17, 2003 at 07:00:18 Pacific
Subject: Read a txt file in Vb
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


Report Offensive Message For Removal


Response Number 1
Name: AlwaysWillingToLearn
Date: September 17, 2003 at 08:48:04 Pacific
Subject: Read a txt file in Vb
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 2
Name: AlwaysWillingToLearn
Date: September 17, 2003 at 08:50:38 Pacific
Subject: Read a txt file in Vb
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: AlwaysWillingToLearn
Date: September 17, 2003 at 09:27:22 Pacific
Subject: Read a txt file in Vb
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Read a txt file in Vb

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge