Home All Groups Group Topic Archive Search About

Reading a text file from the internet

Author
22 Nov 2007 2:55 AM
Rob M
Hi all,

Does anyone know how to load a text file for input from the internet?  I'm
looking for the *internet* equivalent of this:

open "c:\Whatever.txt" for input as #1
line input #1, VariableName

So, basically what I want to do is this:

open "http://whatever.com/Whatever.txt" for input as #1
line input #1, Variable Name

Can this be done in VB6?

Many thanks!
Rob

Author
22 Nov 2007 4:09 AM
Stefan Berglund
On Thu, 22 Nov 2007 02:55:52 GMT, "Rob M" <whate***@address.com> wrote:
in <Yc61j.19886$PE.13000@pd7urf1no>

Show quote
>Hi all,
>
>Does anyone know how to load a text file for input from the internet?  I'm
>looking for the *internet* equivalent of this:
>
>open "c:\Whatever.txt" for input as #1
>line input #1, VariableName
>
>So, basically what I want to do is this:
>
>open "http://whatever.com/Whatever.txt" for input as #1
>line input #1, Variable Name
>
>Can this be done in VB6?
>
>Many thanks!
>Rob
>

This comes really close.  Note that it only reads 12 bytes:


Public Function GetDownloadVersion(ByVal sURL As String) As String

  Dim hOpen As Long: hOpen = InternetOpen("It's ShowTime!", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
  Dim hOpenUrl As Long: hOpenUrl = InternetOpenUrl(hOpen, "http://" & sURL, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)

  Dim lNumberOfBytesRead As Long
  Do While (Not CBool(lNumberOfBytesRead))
    Dim sReadBuffer As String * 12: sReadBuffer = vbNullString
    Call InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
    Dim sBuffer As String: sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
    DoEvents
  Loop

  If (hOpenUrl <> 0) Then InternetCloseHandle (hOpenUrl)
  If (hOpen <> 0) Then InternetCloseHandle (hOpen)

  GetDownloadVersion = sBuffer
  If (Len(sBuffer) = 0) Then MsgBox "The server is not responding." & vbCrLf & vbCrLf & "Please check your Internet connection and try again.", vbExclamation, "Connection Error "

End Function

hehe...my spell checker offered Echinoderms for GetDownloadVersion

---
Stefan Berglund
Author
22 Nov 2007 2:53 PM
PeterD
Show quote
On Thu, 22 Nov 2007 02:55:52 GMT, "Rob M" <whate***@address.com>
wrote:

>Hi all,
>
>Does anyone know how to load a text file for input from the internet?  I'm
>looking for the *internet* equivalent of this:
>
>open "c:\Whatever.txt" for input as #1
>line input #1, VariableName
>
>So, basically what I want to do is this:
>
>open "http://whatever.com/Whatever.txt" for input as #1
>line input #1, Variable Name
>
>Can this be done in VB6?
>
>Many thanks!
>Rob
>

The INet control will read an entire file into a buffer for you. You
can then parse the buffer to get the file's contents line by line.
Author
23 Nov 2007 3:48 AM
Rob M
Many thanks for both responses!

Can you direct me to an example of how to do this using the INet control?
I've played around with it and have tried to find an example using Google,
but have had no luck.  I've done absolutely no internet programming with VB.

Thanks,
Rob


Show quote
"PeterD" <pet***@hipson.net> wrote in message
news:ls5bk3purhn52eh6hjl1cvoqsqde0u048n@4ax.com...
> On Thu, 22 Nov 2007 02:55:52 GMT, "Rob M" <whate***@address.com>
> wrote:
>
>>Hi all,
>>
>>Does anyone know how to load a text file for input from the internet?  I'm
>>looking for the *internet* equivalent of this:
>>
>>open "c:\Whatever.txt" for input as #1
>>line input #1, VariableName
>>
>>So, basically what I want to do is this:
>>
>>open "http://whatever.com/Whatever.txt" for input as #1
>>line input #1, Variable Name
>>
>>Can this be done in VB6?
>>
>>Many thanks!
>>Rob
>>
>
> The INet control will read an entire file into a buffer for you. You
> can then parse the buffer to get the file's contents line by line.
Author
23 Nov 2007 4:53 AM
Kevin Provance
This example on Randy Birch's site should do the trick for you:
http://vbnet.mvps.org/code/internet/urldownloadtofilenocache.htm

- Kev

"Rob M" <whate***@address.com> wrote in message
news:z4s1j.23836$cD.12448@pd7urf2no...
Show quote
| Many thanks for both responses!
|
| Can you direct me to an example of how to do this using the INet control?
| I've played around with it and have tried to find an example using Google,
| but have had no luck.  I've done absolutely no internet programming with
VB.
|
| Thanks,
| Rob
|
|
| "PeterD" <pet***@hipson.net> wrote in message
| news:ls5bk3purhn52eh6hjl1cvoqsqde0u048n@4ax.com...
| > On Thu, 22 Nov 2007 02:55:52 GMT, "Rob M" <whate***@address.com>
| > wrote:
| >
| >>Hi all,
| >>
| >>Does anyone know how to load a text file for input from the internet?
I'm
| >>looking for the *internet* equivalent of this:
| >>
| >>open "c:\Whatever.txt" for input as #1
| >>line input #1, VariableName
| >>
| >>So, basically what I want to do is this:
| >>
| >>open "http://whatever.com/Whatever.txt" for input as #1
| >>line input #1, Variable Name
| >>
| >>Can this be done in VB6?
| >>
| >>Many thanks!
| >>Rob
| >>
| >
| > The INet control will read an entire file into a buffer for you. You
| > can then parse the buffer to get the file's contents line by line.
|
|
Author
23 Nov 2007 3:03 PM
PeterD
On Fri, 23 Nov 2007 03:48:47 GMT, "Rob M" <whate***@address.com>
wrote:

>Many thanks for both responses!
>
>Can you direct me to an example of how to do this using the INet control?
>I've played around with it and have tried to find an example using Google,
>but have had no luck.  I've done absolutely no internet programming with VB.
>
>Thanks,
>Rob

Put an INet control on your frmMain form. If you use a different form
or name, change the code to match.

    Dim b() As Byte

    On Error GoTo ErrorInFunction

    strInputFileName = "myfile.txt" ' Your file name
    strSilentURL = "http://www.myURL.com/files/" & strInputFileName

    frmMain.INet1.URL = strSilentURL
    frmMain.INet1.Protocol = icHTTP

    ' Put result of download into buffer b().
    b() = frmMain.INet1.OpenURL(frmMain.INet1.URL, icByteArray)


Once this codes runs, the buffer will contain the file's contents. YOu
can then use a Put... to write it to a file, or convert the byte array
to a string (or array of strings) if you wish.
Author
26 Nov 2007 12:14 AM
Rob M
Thanks again to everyone!

I'm getting closer.  The solutions from Peter and Kevin downloaded the
files.  The only issue now is that I'd like to download text files, but the
line breaks aren't coming through properly - they turn into little square
boxes.  Is there any way to download text files so they retain their
formatting?

Thanks,
Rob



Show quote
"PeterD" <pet***@hipson.net> wrote in message
news:kkqdk31utjrj8oas6422iijos2fdbuptkt@4ax.com...
> On Fri, 23 Nov 2007 03:48:47 GMT, "Rob M" <whate***@address.com>
> wrote:
>
>>Many thanks for both responses!
>>
>>Can you direct me to an example of how to do this using the INet control?
>>I've played around with it and have tried to find an example using Google,
>>but have had no luck.  I've done absolutely no internet programming with
>>VB.
>>
>>Thanks,
>>Rob
>
> Put an INet control on your frmMain form. If you use a different form
> or name, change the code to match.
>
>    Dim b() As Byte
>
>    On Error GoTo ErrorInFunction
>
>    strInputFileName = "myfile.txt" ' Your file name
>    strSilentURL = "http://www.myURL.com/files/" & strInputFileName
>
>    frmMain.INet1.URL = strSilentURL
>    frmMain.INet1.Protocol = icHTTP
>
>    ' Put result of download into buffer b().
>    b() = frmMain.INet1.OpenURL(frmMain.INet1.URL, icByteArray)
>
>
> Once this codes runs, the buffer will contain the file's contents. YOu
> can then use a Put... to write it to a file, or convert the byte array
> to a string (or array of strings) if you wish.
Author
25 Nov 2007 11:41 PM
Larry Serflaten
"Rob M" <whate***@address.com> wrote
> Thanks again to everyone!
>
> I'm getting closer.  The solutions from Peter and Kevin downloaded the
> files.  The only issue now is that I'd like to download text files, but the
> line breaks aren't coming through properly - they turn into little square
> boxes.  Is there any way to download text files so they retain their
> formatting?

They are only little square boxes in the method you choose to display
the text.  Perhaps you could choose another method to see different
results.

Can you post a short example to the newsgroup, for all to see?

LFS
Author
26 Nov 2007 1:57 AM
Rob M
I'm using the code exactly as written here:

http://vbnet.mvps.org/index.html?code/internet/urldownloadtofilenocache.htm

Here's where I'm getting confused.  The source file code line in the example
above downloads and opens up absolutely fine in Notepad (that's my litmus
test).  The line breaks are there and the original formatting is preserved.
Here's the original relevant coding line:

sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"

If I replace that line with this one (as an example):

sSourceUrl = "http://uvic.ca"

and open up the saved file in Notepad, the file is full of little squares
instead of line breaks.  Each little square corresponds exactly to a line
break.

So, basically what I'm trying to do using VB is download a text file and
keep the formatting intact, line breaks at all, so that it can be reliably
opened and edited in Notepad.

Thanks everyone,
Rob




Show quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:uJqRzT8LIHA.4476@TK2MSFTNGP06.phx.gbl...
>
> "Rob M" <whate***@address.com> wrote
>> Thanks again to everyone!
>>
>> I'm getting closer.  The solutions from Peter and Kevin downloaded the
>> files.  The only issue now is that I'd like to download text files, but
>> the
>> line breaks aren't coming through properly - they turn into little square
>> boxes.  Is there any way to download text files so they retain their
>> formatting?
>
> They are only little square boxes in the method you choose to display
> the text.  Perhaps you could choose another method to see different
> results.
>
> Can you post a short example to the newsgroup, for all to see?
>
> LFS
>
>
Author
26 Nov 2007 4:48 AM
Steve Gerrard
"Rob M" <whate***@address.com> wrote in message
news:bKp2j.46372$cD.5970@pd7urf2no...
Show quote
> I'm using the code exactly as written here:
>
> http://vbnet.mvps.org/index.html?code/internet/urldownloadtofilenocache.htm
>
> Here's where I'm getting confused.  The source file code line in the example
> above downloads and opens up absolutely fine in Notepad (that's my litmus
> test).  The line breaks are there and the original formatting is preserved.
> Here's the original relevant coding line:
>
> sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"
>
> If I replace that line with this one (as an example):
>
> sSourceUrl = "http://uvic.ca"
>
> and open up the saved file in Notepad, the file is full of little squares
> instead of line breaks.  Each little square corresponds exactly to a line
> break.

Well first of all, that's not a text file, it's an HTML file.

Try starting Notepad, click File, Open..., then enter http://uvic.ca as the file
name. It will open it up directly, giving you:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" etc... with the
little squares in it.

Second of all, a university web site is likely to be on a Unix server, so you
would get Unix style line breaks anyway, not Windows style, that is LF instead
of CRLF.

If you want the text from an HTML file, you should probably be using a DOM
object or other internet component. However, you can replace the Unix newline
with the windows newline by doing something like this, where s is the text
string:

s = Replace(s, vbCrLf, vbLf)
s = Replace(s, vbLf, vbCrLf)

The two step is just in case there are some vbCrLf's already in the file (there
are).
Author
27 Nov 2007 3:34 AM
Rob M
Hi all,

I figured out a solution, although I'm honestly not sure why it works.  I
suspect I don't know enough about text file encoding.

Anyway, if I create an ANSI-encoded text file, upload it to the university
server and then download it using the code from vbnet.mvps.org (below), the
downloaded file has little square boxes instead of line breaks, i.e., the
formatting is lost.  Further, my VB6 code "as is" can't handle it.  However,
if I create a unicode-encoded text file, and repeat the process, everything
works fine.  Maybe that will help someone down the road.

Thanks to all who helped out.  I'm always amazed at and grateful for
people's willingness to lend a hand.

Cheers,
Rob



Show quote
"Steve Gerrard" <mynameh***@comcast.net> wrote in message
news:TqGdnWVCgOKfzdfanZ2dnUVZ_ommnZ2d@comcast.com...
>
> "Rob M" <whate***@address.com> wrote in message
> news:bKp2j.46372$cD.5970@pd7urf2no...
>> I'm using the code exactly as written here:
>>
>> http://vbnet.mvps.org/index.html?code/internet/urldownloadtofilenocache.htm
>>
>> Here's where I'm getting confused.  The source file code line in the
>> example above downloads and opens up absolutely fine in Notepad (that's
>> my litmus test).  The line breaks are there and the original formatting
>> is preserved. Here's the original relevant coding line:
>>
>> sSourceUrl = "http://vbnet.mvps.org/code/faq/fileloadtext.htm"
>>
>> If I replace that line with this one (as an example):
>>
>> sSourceUrl = "http://uvic.ca"
>>
>> and open up the saved file in Notepad, the file is full of little squares
>> instead of line breaks.  Each little square corresponds exactly to a line
>> break.
>
> Well first of all, that's not a text file, it's an HTML file.
>
> Try starting Notepad, click File, Open..., then enter http://uvic.ca as
> the file name. It will open it up directly, giving you:
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" etc... with
> the little squares in it.
>
> Second of all, a university web site is likely to be on a Unix server, so
> you would get Unix style line breaks anyway, not Windows style, that is LF
> instead of CRLF.
>
> If you want the text from an HTML file, you should probably be using a DOM
> object or other internet component. However, you can replace the Unix
> newline with the windows newline by doing something like this, where s is
> the text string:
>
> s = Replace(s, vbCrLf, vbLf)
> s = Replace(s, vbLf, vbCrLf)
>
> The two step is just in case there are some vbCrLf's already in the file
> (there are).
>
>
>
>
Author
27 Nov 2007 12:51 PM
Jim Mack
Rob M wrote:
> Hi all,
>
> Anyway, if I create an ANSI-encoded text file, upload it to the
> university server and then download it using the code from
> vbnet.mvps.org (below), the downloaded file has little square boxes
> instead of line breaks, i.e., the formatting is lost.  Further, my
> VB6 code "as is" can't handle it.  However, if I create a
> unicode-encoded text file, and repeat the process, everything works
> fine.  Maybe that will help someone down the road.

Sounds as if you're using an ASCII mode file transfer in the upload
direction, which will 'helpfully' switch all newlines to match the
target system, then a BINARY mode for the download, which ignores all
that.

It's my experience that ASCII mode is best avoided. Explicitly
construct the file for the target audience, then use BINARY mode
throughout.

--
    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com
Author
28 Nov 2007 6:52 AM
Rob M
That explains it!  Thanks.  After experimenting with various ASCII/binary
upload/download combinations, you're absolutely right.  I appreciate the
info.

Cheers,
Rob


Show quote
"Jim Mack" <jmack@mdxi.nospam.com> wrote in message
news:%23xi2wQPMIHA.1164@TK2MSFTNGP02.phx.gbl...
> Rob M wrote:
>> Hi all,
>>
>> Anyway, if I create an ANSI-encoded text file, upload it to the
>> university server and then download it using the code from
>> vbnet.mvps.org (below), the downloaded file has little square boxes
>> instead of line breaks, i.e., the formatting is lost.  Further, my
>> VB6 code "as is" can't handle it.  However, if I create a
>> unicode-encoded text file, and repeat the process, everything works
>> fine.  Maybe that will help someone down the road.
>
> Sounds as if you're using an ASCII mode file transfer in the upload
> direction, which will 'helpfully' switch all newlines to match the
> target system, then a BINARY mode for the download, which ignores all
> that.
>
> It's my experience that ASCII mode is best avoided. Explicitly
> construct the file for the target audience, then use BINARY mode
> throughout.
>
> --
>    Jim Mack
>    MicroDexterity Inc
>    www.microdexterity.com
>
Author
26 Nov 2007 12:29 AM
Bob Butler
"Rob M" <whate***@address.com> wrote in message
news:zdo2j.46152$cD.1644@pd7urf2no...
> Thanks again to everyone!
>
> I'm getting closer.  The solutions from Peter and Kevin downloaded the
> files.  The only issue now is that I'd like to download text files, but
> the line breaks aren't coming through properly - they turn into little
> square boxes.  Is there any way to download text files so they retain
> their formatting?

How are you displaying them?  Many files have either a CR or an LF character
as a line break and things like multiline text boxes require both in
sequence. You need to look at the data to see what character(s) delimit
lines.

AddThis Social Bookmark Button