J'ai essayé d'utiliser l'API wininet.dll pour rechercher un site FTP pour les fichiers correspondants, mais cela ne fonctionne pas pour une raison quelconque. Voici la méthode que j'ai utilisée.Recherche et récupération de noms de fichiers à partir d'un site FTP en utilisant wininet.dll api
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
Voici la fonction 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
Tout ce que je continue à recevoir est "" et ".." pour les fichiers renvoyés par la fonction SearchForFiles. Est-ce que je fais quelque chose de façon incorrecte?
Merci!
Fix mon code! Qu'avez-vous essayé? Où ça casse? Pouvez-vous passer au travers? – knight666
Il ne casse pas. C'est juste qu'il ne lit jamais les fichiers .txt correspondants assis sur le site FTP sous le répertoire/root/testdir/testdir2/ – zSynopsis
Lors de la soumission du code vb la prochaine fois, sachez que la coloration syntaxique n'aime pas les apostrophes pour commenter. J'ai pris la liberté de changer tous les commentaires ''comment' à'' '// ', ce qui est mieux compris (et ne casserait pas non plus votre code) – xtofl