我一直在尝试使用 wininet.dll api 来搜索 ftp 站点以查找匹配的文件,但由于某种原因它无法正常工作。这是我一直在使用的方法。
Private Sub DoStuff()
Dim hConnection As Long, hOpen As Long, sOrgPath As String, lRes As Long
Dim scUserAgent$
scUserAgent$ = "vb wininet"
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hConnection = InternetConnect(hOpen, mServer$, INTERNET_DEFAULT_FTP_PORT, mUserid$, mPassword$, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
''//set the current directory to 'root/testdir/testdir2'
FtpSetCurrentDirectory hConnection, "testdir/testdir2"
ReDim matchingFiles$(1)
Call SearchForFiles(hConnection, ".txt", matchingFiles$)
''//Close the connections
InternetCloseHandle hConnection
InternetCloseHandle hOpen
End Sub
这是 SearchForFiles 函数
Public Sub SearchForFiles(hConnection As Long, fileExtension$, matchingFiles$())
Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
Dim i%
ReDim matchingFiles$(1)
i% = 1
''//create a buffer
pData.cFileName = String(MAX_PATH, 0)
''//find the first file
hFind = FtpFindFirstFile(hConnection, "*." + fileExtension$, pData, 0, 0)
''//if there is no file, then exit sub
If hFind = 0 Then Exit Sub
''//show the filename
matchingFiles$(i%) = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
Do
i% = i% + 1
''//create a buffer
pData.cFileName = String(MAX_PATH, 0)
''//find the next file
lRet = InternetFindNextFile(hFind, pData)
''//if there is no next file, exit do
If lRet = 0 Then Exit Do
''//show the filename
ReDim Preserve matchingFiles$(UBound(matchingFiles) + 1)
matchingFiles$(i%) = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
Loop
''//close the search handle
InternetCloseHandle hFind
End Sub
我一直得到的是“。” “..”表示从 SearchForFiles 函数返回的文件。我做错了什么吗?
谢谢!