msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

File Reading

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
HardySpicer



Joined: 20 Nov 2007
Posts: 5

PostPosted: Fri Nov 23, 2007 3:45 am    Post subject: File Reading Reply with quote

Ok - I have a text file (say c:\text.txt" with a certain number of
names all in a vertical list (I suppose you could say with return-line
feed between them in ascii)

I can read the first line with this function

Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String

Dim strContents As String
Dim objReader As StreamReader
Try

objReader = New StreamReader(FullPath)
strContents = objReader.ReadLine
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function


ie something like

filenamestring=GetFileContents("c\test.txt")
but how to advance to read line 2,3 etc?

Thanks

Hardy

Archived from group: microsoft>public>vb>syntax
Back to top
View user's profile Send private message
Eligio Morgado



Joined: 23 Nov 2007
Posts: 3

PostPosted: Fri Nov 23, 2007 1:47 pm    Post subject: Re: File Reading Reply with quote

Hello,

Try something like this:

Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String

Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = ""
Do While Not objReader.AtEndOfStream
strContents = strContents + objReader.ReadLine + vbCrLf
Loop
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function

Anyway, if you want to return the full content of the file, you can do
it easier and more efficient in that way:

Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String

Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = objReader.ReadAll
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function

I hope this will help you.



Eligio Morgado.

HardySpicer escribió:
> Ok - I have a text file (say c:\text.txt" with a certain number of
> names all in a vertical list (I suppose you could say with return-line
> feed between them in ascii)
>
> I can read the first line with this function
>
> Public Function GetFileContents(ByVal FullPath As String, _
> Optional ByRef ErrInfo As String = "") As String
>
> Dim strContents As String
> Dim objReader As StreamReader
> Try
>
> objReader = New StreamReader(FullPath)
> strContents = objReader.ReadLine
> objReader.Close()
> Return strContents
> Catch Ex As Exception
> ErrInfo = Ex.Message
> End Try
> End Function
>
>
> ie something like
>
> filenamestring=GetFileContents("c\test.txt")
> but how to advance to read line 2,3 etc?
>
> Thanks
>
> Hardy
Back to top
View user's profile Send private message
HardySpicer



Joined: 20 Nov 2007
Posts: 5

PostPosted: Fri Nov 23, 2007 6:38 am    Post subject: Re: File Reading Reply with quote

On Nov 23, 8:47 pm, Eligio Morgado wrote:
> Hello,
>
> Try something like this:
>
> Public Function GetFileContents(ByVal FullPath As String, _
> Optional ByRef ErrInfo As String = "") As String
>
> Dim strContents As String
> Dim objReader As StreamReader
> Try
> objReader = New StreamReader(FullPath)
> strContents = ""
> Do While Not objReader.AtEndOfStream
> strContents = strContents + objReader.ReadLine + vbCrLf
> Loop
> objReader.Close()
> Return strContents
> Catch Ex As Exception
> ErrInfo = Ex.Message
> End Try
> End Function
>
> Anyway, if you want to return the full content of the file, you can do
> it easier and more efficient in that way:
>
> Public Function GetFileContents(ByVal FullPath As String, _
> Optional ByRef ErrInfo As String = "") As String
>
> Dim strContents As String
> Dim objReader As StreamReader
> Try
> objReader = New StreamReader(FullPath)
> strContents = objReader.ReadAll
> objReader.Close()
> Return strContents
> Catch Ex As Exception
> ErrInfo = Ex.Message
> End Try
> End Function
>
> I hope this will help you.
>
> Eligio Morgado.
>
> HardySpicer escribió:
>
> > Ok - I have a text file (say c:\text.txt" with a certain number of
> > names all in a vertical list (I suppose you could say with return-line
> > feed between them in ascii)
>
> > I can read the first line with this function
>
> > Public Function GetFileContents(ByVal FullPath As String, _
> > Optional ByRef ErrInfo As String = "") As String
>
> > Dim strContents As String
> > Dim objReader As StreamReader
> > Try
>
> > objReader = New StreamReader(FullPath)
> > strContents = objReader.ReadLine
> > objReader.Close()
> > Return strContents
> > Catch Ex As Exception
> > ErrInfo = Ex.Message
> > End Try
> > End Function
>
> > ie something like
>
> > filenamestring=GetFileContents("c\test.txt")
> > but how to advance to read line 2,3 etc?
>
> > Thanks
>
> > Hardy

Ok thanks but my text file has this format

file1.mp3
file2.mp3
file3.mp3

etc

and I need to access the individual files.I can get the whole list ok
but how to split it up!

Hardy
Back to top
View user's profile Send private message
Eligio Morgado



Joined: 23 Nov 2007
Posts: 3

PostPosted: Fri Nov 23, 2007 6:19 pm    Post subject: Re: File Reading Reply with quote

In my first code you have the clue for individual proccess. Check it out:

Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String

Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = ""
Do While Not objReader.AtEndOfStream
MsgBox objReader.ReadLine 'Here you are one file line
Loop
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function

HardySpicer escribió:
> On Nov 23, 8:47 pm, Eligio Morgado wrote:
>> Hello,
>>
>> Try something like this:
>>
>> Public Function GetFileContents(ByVal FullPath As String, _
>> Optional ByRef ErrInfo As String = "") As String
>>
>> Dim strContents As String
>> Dim objReader As StreamReader
>> Try
>> objReader = New StreamReader(FullPath)
>> strContents = ""
>> Do While Not objReader.AtEndOfStream
>> strContents = strContents + objReader.ReadLine + vbCrLf
>> Loop
>> objReader.Close()
>> Return strContents
>> Catch Ex As Exception
>> ErrInfo = Ex.Message
>> End Try
>> End Function
>>
>> Anyway, if you want to return the full content of the file, you can do
>> it easier and more efficient in that way:
>>
>> Public Function GetFileContents(ByVal FullPath As String, _
>> Optional ByRef ErrInfo As String = "") As String
>>
>> Dim strContents As String
>> Dim objReader As StreamReader
>> Try
>> objReader = New StreamReader(FullPath)
>> strContents = objReader.ReadAll
>> objReader.Close()
>> Return strContents
>> Catch Ex As Exception
>> ErrInfo = Ex.Message
>> End Try
>> End Function
>>
>> I hope this will help you.
>>
>> Eligio Morgado.
>>
>> HardySpicer escribió:
>>
>>> Ok - I have a text file (say c:\text.txt" with a certain number of
>>> names all in a vertical list (I suppose you could say with return-line
>>> feed between them in ascii)
>>> I can read the first line with this function
>>> Public Function GetFileContents(ByVal FullPath As String, _
>>> Optional ByRef ErrInfo As String = "") As String
>>> Dim strContents As String
>>> Dim objReader As StreamReader
>>> Try
>>> objReader = New StreamReader(FullPath)
>>> strContents = objReader.ReadLine
>>> objReader.Close()
>>> Return strContents
>>> Catch Ex As Exception
>>> ErrInfo = Ex.Message
>>> End Try
>>> End Function
>>> ie something like
>>> filenamestring=GetFileContents("c\test.txt")
>>> but how to advance to read line 2,3 etc?
>>> Thanks
>>> Hardy
>
> Ok thanks but my text file has this format
>
> file1.mp3
> file2.mp3
> file3.mp3
>
> etc
>
> and I need to access the individual files.I can get the whole list ok
> but how to split it up!
>
> Hardy
Back to top
View user's profile Send private message
Bob Butler



Joined: 04 Oct 2007
Posts: 1081

PostPosted: Fri Nov 23, 2007 11:42 am    Post subject: Re: File Reading Reply with quote

"HardySpicer" wrote in message @d21g2000prf.googlegroups.com...
> Ok - I have a text file (say c:\text.txt" with a certain number of
> names all in a vertical list (I suppose you could say with return-line
> feed between them in ascii)
>
> I can read the first line with this function
>
> Public Function GetFileContents(ByVal FullPath As String, _
> Optional ByRef ErrInfo As String = "") As String
>
> Dim strContents As String
> Dim objReader As StreamReader
> Try


--
You need to ask in a newsgroup with "dotnet" in the name. This group is for
VB 6.0 and earlier and does not include VB.Net or VB 200x.
Back to top
View user's profile Send private message
HardySpicer



Joined: 20 Nov 2007
Posts: 5

PostPosted: Fri Nov 23, 2007 1:58 pm    Post subject: Re: File Reading Reply with quote

On Nov 24, 1:19 am, Eligio Morgado wrote:
> In my first code you have the clue for individual proccess. Check it out:
>
> Public Function GetFileContents(ByVal FullPath As String, _
> Optional ByRef ErrInfo As String = "") As String
>
> Dim strContents As String
> Dim objReader As StreamReader
> Try
> objReader = New StreamReader(FullPath)
> strContents = ""
> Do While Not objReader.AtEndOfStream
> MsgBox objReader.ReadLine 'Here you are one file line
> Loop
> objReader.Close()
> Return strContents
> Catch Ex As Exception
> ErrInfo = Ex.Message
> End Try
> End Function
>
> HardySpicer escribió:
>
> > On Nov 23, 8:47 pm, Eligio Morgado wrote:
> >> Hello,
>
> >> Try something like this:
>
> >> Public Function GetFileContents(ByVal FullPath As String, _
> >> Optional ByRef ErrInfo As String = "") As String
>
> >> Dim strContents As String
> >> Dim objReader As StreamReader
> >> Try
> >> objReader = New StreamReader(FullPath)
> >> strContents = ""
> >> Do While Not objReader.AtEndOfStream
> >> strContents = strContents + objReader.ReadLine + vbCrLf
> >> Loop
> >> objReader.Close()
> >> Return strContents
> >> Catch Ex As Exception
> >> ErrInfo = Ex.Message
> >> End Try
> >> End Function
>
> >> Anyway, if you want to return the full content of the file, you can do
> >> it easier and more efficient in that way:
>
> >> Public Function GetFileContents(ByVal FullPath As String, _
> >> Optional ByRef ErrInfo As String = "") As String
>
> >> Dim strContents As String
> >> Dim objReader As StreamReader
> >> Try
> >> objReader = New StreamReader(FullPath)
> >> strContents = objReader.ReadAll
> >> objReader.Close()
> >> Return strContents
> >> Catch Ex As Exception
> >> ErrInfo = Ex.Message
> >> End Try
> >> End Function
>
> >> I hope this will help you.
>
> >> Eligio Morgado.
>
> >> HardySpicer escribió:
>
> >>> Ok - I have a text file (say c:\text.txt" with a certain number of
> >>> names all in a vertical list (I suppose you could say with return-line
> >>> feed between them in ascii)
> >>> I can read the first line with this function
> >>> Public Function GetFileContents(ByVal FullPath As String, _
> >>> Optional ByRef ErrInfo As String = "") As String
> >>> Dim strContents As String
> >>> Dim objReader As StreamReader
> >>> Try
> >>> objReader = New StreamReader(FullPath)
> >>> strContents = objReader.ReadLine
> >>> objReader.Close()
> >>> Return strContents
> >>> Catch Ex As Exception
> >>> ErrInfo = Ex.Message
> >>> End Try
> >>> End Function
> >>> ie something like
> >>> filenamestring=GetFileContents("c\test.txt")
> >>> but how to advance to read line 2,3 etc?
> >>> Thanks
> >>> Hardy
>
> > Ok thanks but my text file has this format
>
> > file1.mp3
> > file2.mp3
> > file3.mp3
>
> > etc
>
> > and I need to access the individual files.I can get the whole list ok
> > but how to split it up!
>
> > Hardy

Many thanks.
Hardy
Back to top
View user's profile Send private message
Jeff Johnson



Joined: 04 Oct 2007
Posts: 1327

PostPosted: Sat Nov 24, 2007 2:33 am    Post subject: Re: File Reading Reply with quote

"HardySpicer" wrote in message @d21g2000prf.googlegroups.com...

> Dim objReader As StreamReader

[Canned response]
This is a VB "classic" newsgroup. Questions about VB.NET (including VB 200x,
which has dropped .NET from its name) are off-topic here.

Please ask .NET questions in newsgroups with "dotnet" in their names. The
*.vb.* groups are for VB6 and earlier. If you don't see the *.dotnet.*
groups on your news server, connect directly to the Microsoft server:
msnews.microsoft.com.

For questions specific to the VB.NET language, use this group:
microsoft.public.dotnet.languages.vb

Please note that things like controls and data access, which have their own
subgroups in the Classic VB hierarchy, are not language-specific in .NET, so
you should look for groups like these:
microsoft.public.dotnet.framework.windowsforms.controls
microsoft.public.dotnet.framework.adonet
(Note that "vb" is not present in the group name.)

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group