what would be the best way to see if a file exists online?
thanks,
Dimava
Printable View
what would be the best way to see if a file exists online?
thanks,
Dimava
VB Code:
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 Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean Dim lngRetVal As Long lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0) If lngRetVal = 0 Then DownloadFile = True End Function Private Sub Form_Load() 'example by Matthew Gates ([email protected]) DownloadFile "http://www.allapi.net", "c:\allapi.htm" End Sub
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
Fair enough. :)
VB Code:
Const scUserAgent = "API-Guide test program" Const INTERNET_OPEN_TYPE_DIRECT = 1 Const INTERNET_OPEN_TYPE_PROXY = 3 Const INTERNET_FLAG_RELOAD = &H80000000 Const sURL = "http://www.microsoft.com/index.htm" 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 Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer 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 Private Sub Form_Load() 'KPD-Team 1999 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] Dim hOpen As Long, hFile As Long, sBuffer As String, Ret As Long 'Create a buffer for the file we're going to download sBuffer = Space(1000) 'Create an internet connection hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0) 'Open the url hFile = InternetOpenUrl(hOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&) [b][i] 'Read the first 1000 bytes of the file[/i][/b] InternetReadFile hFile, sBuffer, 1000, Ret 'clean up InternetCloseHandle hFile InternetCloseHandle hOpen 'Show our file MsgBox sBuffer End Sub
HTH
Regards
KayJay
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
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