7,759
社区成员
发帖
与我相关
我的任务
分享
Dim pData As WIN32_FIND_DATA
Dim lngHINet As Long
Dim strFile As String
pData.cFileName = String(260, 0)
lngHINet = FtpFindFirstFile(lnginetconn, "/DelEcc/conf/condpei.txt", pData, 0, 0)
strFile = Left(pData.cFileName, InStr(1, pData.cFileName, Chr(0)) - 1)
....
'======================================
' 检索指定的路径
'======================================
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 dwContent As Long) As Long
'======================================
' 继续检索下一个路径
'======================================
Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
'===============================================
' 从FTP中取得指定的目录下的内容
'===============================================
Private Sub Sample()
Dim hOpen As Long 'InternetServer的Handle
Dim hConnection As Long 'InternetSession的Handle
Dim result As Long
hOpen = 0
hConnection = 0
Dim hFind As Long
Dim w32FindData As WIN32_FIND_DATA
Dim strFile As String
Dim FileList() As String '文件名一栏
Dim cnt As Long
cnt = -1
'取得InternetServer的Handle - hOpen
hOpen = InternetOpen("FTPSample", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
If (hOpen <> 0) Then 'Handle取得成功
'取得InternetSession的Handle(连接FTPServer) - hConnection
hConnection = InternetConnect(hOpen, "192.168.45.12", INTERNET_INVALID_PORT_NUMBER, _
"UserName", "Password", INTERNET_SERVICE_FTP, 0, 0)
If (hConnection <> 0) Then '连接成功
'改变FTPServer的CurrentDirectory
result = FtpSetCurrentDirectory(hConnection, "/home/monitor/")
If (result <> 0) Then '变更成功
'取得文件一览
hFind = FtpFindFirstFile(hConnection, "*.*", w32FindData, INTERNET_FLAG_RELOAD, 0)
If (hFind = 0) Then
MsgBox "文件名取得失败" & Err.LastDllError
Else
Do
strFile = Left(w32FindData.cFileName, InStr(w32FindData.cFileName, vbNullChar) - 1)
strFile = Mid(strFile, InStrRev(strFile, " ") + 1) '删除文件名中无用的字符
If ((w32FindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = &H10) Then
strFile = strFile & "/" '如果取得的是目录,在目录后面加上/
End If
cnt = cnt + 1
ReDim Preserve FileList(cnt)
FileList(cnt) = strFile '将取得的文件名或者目录名,追加到文件名一览中
Loop Until InternetFindNextFile(hFind, w32FindData) = 0 '取得下一个文件名
End If
Else
MsgBox "目录移动失败" & Err.LastDllError
End If
Else
MsgBox "FTPServer连接失败" & Err.LastDllError
End If
Else
MsgBox "FTPServer连接失败" & Err.LastDllError
End If
'关闭InternetSession
If (hConnection <> 0) Then InternetCloseHandle hConnection
'关闭InternetServer
If (hOpen <> 0) Then InternetCloseHandle hOpen
End Sub