| Listing 2 WinInet calls can seem deep. That is, you need to drill down from one call through another, as you "open" the Internet, connect to a server, and finally start interacting with the server. At each step, you're offered handles for use in the next step. You must manage these handles carefully and release them when through, to avoid troubles down the road. Option Explicit ' Required WinInet API calls and structures Private Declare Function InternetOpen Lib _ "wininet.dll" Alias "InternetOpenA" (ByVal _ lpszAgent As String, ByVal dwAccessType As _ Long, ByVal lpszProxyName As String, ByVal _ lpszProxyBypass As String, ByVal dwFlags As _ Long) As Long Private Declare Function InternetConnect Lib _ "wininet.dll" Alias "InternetConnectA" _ (ByVal hInternetSession As Long, ByVal _ lpszServerName As String, ByVal nServerPort _ As Integer, ByVal lpszUsername As String, _ ByVal lpszPassword As String, ByVal _ dwService As Long, ByVal dwFlags As _ Long, ByVal dwContext As Long) As Long Private Declare Function InternetCloseHandle _ Lib "wininet.dll" (ByVal hInet As Long) As _ Long Private Declare Function FtpFindFirstFile Lib _ "WinInet" Alias "FtpFindFirstFileA" (ByVal _ hFtp As Long, ByVal lpszSearchFile As _ String, lpFindFileData As WIN32_FIND_DATA, _ ByVal dwFlags As Long, ByVal _ dwContext As Long) As Long Private Const MAX_PATH = 260 Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As Currency 'FILETIME ftLastAccessTime As Currency 'FILETIME ftLastWriteTime As Currency 'FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * MAX_PATH cAlternate As String * 14 End Type ' Indicates to use config info from registry Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' Number of the TCP/IP port on the server to ' connect to. Private Const INTERNET_DEFAULT_FTP_PORT = 21 ' Type of service to access. Private Const INTERNET_SERVICE_FTP = 1 ' Read from wire even if locally cached Private Const INTERNET_FLAG_RELOAD = &H80000000 Public Function FtpFileExists(ByVal Server As _ String, ByVal UrlPath As String, Optional _ UserName As String = "", Optional Password As String = "") As Boolean Dim hInetSess As Long Dim hInetConnect As Long Dim hInetFile As Long Dim wfd As WIN32_FIND_DATA Const Agent As String = "VBPJ Pro" ' Open an internet session hInetSess = InternetOpen(Agent, _ INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, vbNullString, 0&) If hInetSess Then ' Connect to host hInetConnect = InternetConnect(hInetSess, _ Server, INTERNET_DEFAULT_FTP_PORT, _ UserName, Password, _ INTERNET_SERVICE_FTP, 0, 0) If hInetConnect Then ' Search for matching filespec hInetFile = FtpFindFirstFile _ (hInetConnect, UrlPath, wfd, _ INTERNET_FLAG_RELOAD, 0) If hInetFile Then FtpFileExists = True Call InternetCloseHandle(hInetFile) End If Call InternetCloseHandle(hInetConnect) End If Call InternetCloseHandle(hInetSess) End If End Function |
Check FTP Server for Specific File |
Freelance ASP PHP web development | Web developer India Web development India| Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design