'-----FTP連線(使用WinINet.DLL)
Dim session As Long ' 連線環境
Dim server As Long ' 連線登入
Dim hFile As Long ' 搜尋結果
Dim blnRC As Long ' 載檔結果
Dim udtWFD As WIN32_FIND_DATA ' 傳回的資料
' 連線環境設定
session = InternetOpen(gHost, INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
'------Wininet.dll的FTP連線
If session <> 0 Then
'server = InternetConnect(session, Adresa, Port, ID, Pass, INTERNET_SERVICE_FTP, Service, &H0)
server = InternetConnect(session, gHost, "21", gUserName, gPassword, 1, &H8000000, &H0)
If server <> 0 Then
'------日報表-----------
ProgBar.Min = 0: ProgBar.Value = 0: MSGrid2.Rows = 1 ' 進度表
'hFile = FtpFindFirstFile(server, adr, udtWFD, INTERNET_FLAG_RELOAD Or INTERNET_FLAG_NO_CACHE_WRITE, 0&)
hFile = FtpFindFirstFile(server, gCls1Path & "1/paper/" & Txt(2).Text & TxtShow(2).Text & "*.*", udtWFD, 0, 0)
If hFile Then
' 將檔名置入Strarr陣列內
FileCount = 0
Do
ReDim Preserve Strarr(FileCount)
strFile = Left(udtWFD.cFileName, InStr(1, udtWFD.cFileName, Chr(0)) - 1)
If Len(strFile) > 0 Then
If Not (udtWFD.dwFileAttributes And vbDirectory) Then ' 如果不是資料夾'&H1:唯讀 &H2:隱藏 &H4:系統 &H16:資料夾 &H20:保存 &H80:一般(取消前四種屬性)
Strarr(FileCount) = Left(udtWFD.cFileName, InStr(1, udtWFD.cFileName, Chr(0)) - 1)
FileCount = FileCount + 1
End If
End If
Loop While InternetFindNextFile(hFile, udtWFD)
ProgBar.Max = UBound(Strarr) + 1
MSGrid2.Rows = UBound(Strarr) + 2
For i = 0 To UBound(Strarr)
blnRC = FtpGetFile(server, gCls1Path & "1/paper/" & strFile, App.Path & "\cont\paper\" & strFile, 0, 0, 1, 0)
If blnRC = 1 Then ' 下載成功
DoEvents
ProgBar.Value = ProgBar.Value + 1
MSGrid2.TextMatrix(i + 1, 0) = i + 1
MSGrid2.TextMatrix(i + 1, 1) = "施工日報表"
MSGrid2.TextMatrix(i + 1, 2) = Strarr(i)
MSGrid2.TextMatrix(i + 1, 3) = App.Path & "\cont\paper\" & Strarr(i)
End If
Next i
ProgBar.Value = 0
MSGrid2RowCount = UBound(Strarr) + 1 ' 總筆數 因為是從0開始
End If
InternetCloseHandle hFile ' 關閉搜尋
'-----離開時的關檔
InternetCloseHandle hFile
InternetCloseHandle server
InternetCloseHandle session
'-------模組
'-------FTP連線,使用Wininet.dll--------
' 調用設置環境
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
' 函式與主機連接
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal pub_lngInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
' 取得路徑列表
Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
' 移到搜尋結果的下一筆
Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
' 取檔案
Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
' 關閉連線
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
' 連線方式 1 直接連線、3 透過代理服務器、 0 基於HKEY...下的註冊表數值
Public Const INTERNET_OPEN_TYPE_DIRECT = 1