RSS Forums RSS

Check a file exists on a website

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Reply
Posts: 10
Reputation: No Pain No Gain is an unknown quantity at this point 
Solved Threads: 0
No Pain No Gain No Pain No Gain is offline Offline
Newbie Poster

Check a file exists on a website

  #1  
Jan 12th, 2009
Currently my users download a database outside my app then from within it they can add the database to their main database. Now I want to have the app download and add the database all at once.

If I change the file extension to .txt the code below downloads the database as a text file but saves it as a mdb.

Dim strURL As String
Dim bData() As Byte ' Data variable
Dim intFile As Integer ' FreeFile variable

strURL = "http://www.asite/update1.txt"
intFile = FreeFile()
bData() = Inet1.OpenURL(strURL, icByteArray)
Open App.Path & "\update1.mdb" For Binary Access Write As #intFile
Put #intFile, , bData()
Close #intFile

But how can I check that the file exists on my site?

If the file doesn't exist, i.e. I haven't added the update to my site yet, the code still creates an update1.mdb that is really a text file that starts:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>The page cannot be found</TITLE>
AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,404
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 209
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Check a file exists on a website

  #2  
Jan 12th, 2009
Note:Please use code tags when posting code. You can do this by typing [ code=vb ] (without spaces) put the code in, then end it with [ /code ] (also without spaces).
Attached Files
File Type: zip GetPage.zip (1.8 KB, 15 views)
Reply With Quote  
Posts: 10
Reputation: No Pain No Gain is an unknown quantity at this point 
Solved Threads: 0
No Pain No Gain No Pain No Gain is offline Offline
Newbie Poster

Re: Check a file exists on a website

  #3  
Jan 13th, 2009
Sorry about the code tags.

I've tried the code you attached and edited

Winsock1.RemoteHost = "codefaction.net" to the top level of my site

Send_HTTP_Header Winsock1, "/index.htm" to the path and file name (.txt) of a file I know isn't there

but the app insists the file is "found".

Is this because I am not looking for an HTML file? So when the .txt file is missing it is creates something like when trying to download an .mdb file?
Reply With Quote  
Posts: 2,404
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 209
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Check a file exists on a website

  #4  
Jan 13th, 2009
What the code does, (at least, what it's supposed to do) is use winsock to connect to the web server. Then it sends an HTTP request header to the server, requesting the page found in path "/index.htm", so if the page was http://www.somesite.com/thisthing/my.txt, then that part would be "/thisthing/my.txt". Then, what the web site does, it check for that file. If the file does not exist, the first line that it responds with, is a 404 (in the response header). So, the code checks for a 404 on the first line returned. What you could consider doing, is doing a msgbox on parts(0) right by the if instr(1, parts(0), "404") line (just above it), and see what it's actual line is.
Reply With Quote  
Posts: 10
Reputation: No Pain No Gain is an unknown quantity at this point 
Solved Threads: 0
No Pain No Gain No Pain No Gain is offline Offline
Newbie Poster

Re: Check a file exists on a website

  #5  
Jan 13th, 2009
I've tried writing it to a text box and I get

"HTTP/1.1 400 Bad Request"

Am I doing the remote host bit right? You put codefaction.net does it need anything in front of the site name? I get the same error whether I try a .html, .txt, a file that is there or a file that is missing.
Reply With Quote  
Posts: 10
Reputation: No Pain No Gain is an unknown quantity at this point 
Solved Threads: 0
No Pain No Gain No Pain No Gain is offline Offline
Newbie Poster

Re: Check a file exists on a website

  #6  
Jan 13th, 2009
I've tried variations of the remote host for my site and it never works, always a bad request. My website is probably on a shared server could that be the problem?

If I try your codefaction.net the request works.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 891 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:30 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC