Vérifier - télécharger - supprimer un fichier sur un serveur FTP [Résolu]

le1scorpion1noir 163 Messages postés samedi 19 janvier 2008Date d'inscription 26 août 2014 Dernière intervention - 22 sept. 2010 à 17:49 - Dernière réponse : Renfield 17307 Messages postés mercredi 2 janvier 2002Date d'inscription 18 janvier 2017 Dernière intervention
- 24 sept. 2010 à 05:09
salut a tous
j'ai eu se code super pour envoyer un fichier sur un serveur ftp sans aucun outil a ajouter mai il me manque trois lignes et je compte sur vous pour me les donner
1 - vérifier l'existence d'un fichier
2 - télécharger le fichier
3 - supprimer le fichier

et voila le code a compléter


Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
'Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession 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
Private 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
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private 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
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Const PassiveConnection As Boolean = True

Private Sub cmd_exit_Click()
End
End Sub

Private Sub cmd_send_Click()
    Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    'open an internet connection
    hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    'connect to the FTP server
    hConnection = InternetConnect(hOpen, txt_ftp.Text, INTERNET_DEFAULT_FTP_PORT, txt_login.Text, txt_pw.Text, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    'upload the file 'test.htm'
    FtpPutFile hConnection, "" & App.Path & "\test.txt", "test.txt", FTP_TRANSFER_TYPE_UNKNOWN, 0
    'close the internet connection
    InternetCloseHandle hOpen
    MsgBox ("Sucesful")

End Sub



et merci beaucoup
Afficher la suite 

Votre réponse

4 réponses

Meilleure réponse
Renfield 17307 Messages postés mercredi 2 janvier 2002Date d'inscription 18 janvier 2017 Dernière intervention - 23 sept. 2010 à 04:47
3
Merci
avoir un code qui fonctionne n'est absolument pas une fin en soit, sinon, change de job si tu ne le comprend pas...

a quoi te sert
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)

je vois : FtpDeleteFile
cela semble pas mal, pour supprimer le fichier. Reflex, la doc

idem pour FtpGetFile

et pour FtpFindFirstFile

il te manque un InternetCloseHandle pour le InternetConnect...


j'ajouterai que ton code affiche MsgBox ("Sucesful") sans meme pouvoir t'assurer que le tranfert a ete fait. (faute d'orthographe a successful, au passage)
Aucun test des codes retours n'est fait (ce qui, pour un FtpGetFile pourrais te permettre de tester l'existence...)

Enfin, PROSCRIRE de tes connaissances le mot cle END

Private Sub cmd_exit_Click()
End
End Sub

fais plutot un Unload de tes forms

"" & App.Path & "\test.txt"

a quoi servent les premieres guillemets ?
si mon projet ou mon exe se trouvent a la racine d'un disaue, j'aurais:
F:\\test.txt

un \ en trop...

cote architexture de code, fais plutot une fonction ne faisant que l'envoi, ou la reception, prenant en parametre au moins le nom du fichier.
ainsi, dans ton btnClck tu y fera appel et affichera si besoin la msgbox
ca t'evitera d'avoir a dupliquer ce code au sein de ton projet si tu souhaites uploader un autre fichier.
ca te permettra egalement de reutiliser cette fonction plus simplement dans un autre futur projet


Pour resumer, ne prend pas de codes sans les comprendre, sans prendre le temps de lire la doc... c'est ainsi que tu progresseras


Renfield - Admin CodeS-SourceS - MVP Visual Basic & Spécialiste des RegExp

Merci Renfield 3

Avec quelques mots c'est encore mieux Ajouter un commentaire

Codes Sources a aidé 83 internautes ce mois-ci

Commenter la réponse de Renfield
Meilleure réponse
Renfield 17307 Messages postés mercredi 2 janvier 2002Date d'inscription 18 janvier 2017 Dernière intervention - 24 sept. 2010 à 05:09
3
Merci
tu as lu ma reponse en diagonale ? j'avais detaille...

manque le InternetCloseHandle de hConnection
si FtpFindFirstFile renvoie 0 ca veut pas forcement dire que le fichier n'est pas trouve... juste ERROR. Tester la valeur:
Returns a valid handle for the request if the directory enumeration was started successfully, or returns NULL otherwise. To get a specific error message, call GetLastError. If GetLastError returns ERROR_INTERNET_EXTENDED_ERROR, as in the case where the function finds no matching files, call the InternetGetLastResponseInfo function to retrieve the extended error text, as documented in Handling Errors.


de plus, tu as surement pas tout bien lu dans la doc :
After the calling application has finished using the HINTERNET handle returned by FtpFindFirstFile, it must be closed using the InternetCloseHandle function.



Renfield - Admin CodeS-SourceS - MVP Visual Basic & Spécialiste des RegExp

Merci Renfield 3

Avec quelques mots c'est encore mieux Ajouter un commentaire

Codes Sources a aidé 83 internautes ce mois-ci

Commenter la réponse de Renfield
le1scorpion1noir 163 Messages postés samedi 19 janvier 2008Date d'inscription 26 août 2014 Dernière intervention - 23 sept. 2010 à 18:58
0
Merci
j'ai eu ce code d'ici

mai voila maintenant sa fonctionne très bien
Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
'Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession 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
Private 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
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private 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
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Const PassiveConnection As Boolean = True

Private Sub cmd_exit_Click()
End
End Sub

Private Sub cmd_send_Click()
    Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    'open an internet connection
    hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    'connect to the FTP server
    hConnection = InternetConnect(hOpen, txt_ftp.Text, INTERNET_DEFAULT_FTP_PORT, txt_login.Text, txt_pw.Text, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    'upload the file 'test.htm'
    '------------------------------------------------------------------------------------------------
    FtpPutFile hConnection, "" & App.Path & "\test1.txt", "test1.txt", FTP_TRANSFER_TYPE_UNKNOWN, 0 '#ligne1 upload
    '------------------------------------------------------------------------------------------------
    FtpPutFile hConnection, "C:\\test2.htm", "test2.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0              '#ligne2 télécharger
    '------------------------------------------------------------------------------------------------
    FtpDeleteFile hConnection, "apiguide.htm"                                                       '#ligne3 supprimer
    '------------------------------------------------------------------------------------------------
    Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
    hFind = FtpFindFirstFile(hConnection, "test4.txt", pData, 0, 0)
If hFind = 0 Then
MsgBox "le fichier nexiste pas"
else
msgbox " le fichier existe"
End If                                                                                              '# verification du fichier
    '------------------------------------------------------------------------------------------------
    
    'close the internet connection
    InternetCloseHandle hOpen
    MsgBox ("Successful")

End Sub


ta quelque chose a me faire remarquer avant de valider ta réponse ???
Commenter la réponse de le1scorpion1noir
le1scorpion1noir 163 Messages postés samedi 19 janvier 2008Date d'inscription 26 août 2014 Dernière intervention - 23 sept. 2010 à 19:04
0
Merci
excuse je me suis trompé pour le téléchargement
'-------------------------------------------------------------
FtpGetFile hConnection, "test2.htm", "c:\\test2.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0                           '#ligne2 télécharger
'-------------------------------------------------------------
Commenter la réponse de le1scorpion1noir

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.