Results 1 to 6 of 6

Thread: see if file exists online

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715

    see if file exists online

    what would be the best way to see if a file exists online?

    thanks,

    Dimava
    NXSupport - Your one-stop source for computer help

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    VB Code:
    1. Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    2. Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    3.     Dim lngRetVal As Long
    4.     lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    5.     If lngRetVal = 0 Then DownloadFile = True
    6. End Function
    7. Private Sub Form_Load()
    8.     'example by Matthew Gates ([email protected])
    9.     DownloadFile "http://www.allapi.net", "c:\allapi.htm"
    10. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    thanks,

    but that code Downloads the file and then returns weather or not it exists, if some shareware files are huge, then it'll take a while to validate it. Would it be possible to just download like the first 10bytes or something?


    thanks,

    Dimava
    NXSupport - Your one-stop source for computer help

  4. #4
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Fair enough.

    VB Code:
    1. Const scUserAgent = "API-Guide test program"
    2. Const INTERNET_OPEN_TYPE_DIRECT = 1
    3. Const INTERNET_OPEN_TYPE_PROXY = 3
    4. Const INTERNET_FLAG_RELOAD = &H80000000
    5. Const sURL = "http://www.microsoft.com/index.htm"
    6. Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    7. Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
    8. Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    9. Private Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
    10. Private Sub Form_Load()
    11.     'KPD-Team 1999
    12.     'URL: [url]http://www.allapi.net/[/url]
    13.     'E-Mail: [email][email protected][/email]
    14.  
    15.     Dim hOpen As Long, hFile As Long, sBuffer As String, Ret As Long
    16.     'Create a buffer for the file we're going to download
    17.     sBuffer = Space(1000)
    18.     'Create an internet connection
    19.     hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    20.     'Open the url
    21.     hFile = InternetOpenUrl(hOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
    22.   [b][i]  'Read the first 1000 bytes of the file[/i][/b]
    23.     InternetReadFile hFile, sBuffer, 1000, Ret
    24.     'clean up
    25.     InternetCloseHandle hFile
    26.     InternetCloseHandle hOpen
    27.     'Show our file
    28.     MsgBox sBuffer
    29. End Sub

    HTH

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Thanks a lot, but theres still a problem. Sometimes it returns 404 so that won't work for determining if a file is on the server or not
    NXSupport - Your one-stop source for computer help

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    You can use winsock...

    Just connect to the server and send something like:

    "HEAD /file_to_check.dat HTTP/1.1" & vbNewLine & vbNewLine

    if you get the reply "HTTP/1.1 200 OK", then the file is there, otherwise it's and error (file not found)

    And error 404 means that file was not found...

    Check out this link for the error codes that a server returns:
    http://www.mindflip.com/inet/tcpip/http.html
    Last edited by CVMichael; Feb 21st, 2003 at 02:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width