1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#Const DEBUG_ON = True
' ---------------------------------------------------------
' ------------== Déclaration des API ==--------------------
' ---------------------------------------------------------
' 1. Constantes générales
' ===================
Const MAX_PATH = 260
' 2. Structures
' ==========
' DATE/TIME Structure
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
' FINDFILE Structure
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
cAlternateFileName As String * 13
End Type
' 3. Déclaration Constantes et Functions de l'API Windows Internet/ftp
' =================================================================
Const FTP_TRANSFER_TYPE_ASCII = &H1 ' 0x00000001
Const FTP_TRANSFER_TYPE_BINARY = &H2 ' 0x00000002
Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_DEFAULT_GOPHER_PORT = 70 ' " " gopher "
Const INTERNET_DEFAULT_HTTP_PORT = 80 ' " " HTTP "
Const INTERNET_DEFAULT_HTTPS_PORT = 443 ' " " HTTPS "
Const INTERNET_DEFAULT_SOCKS_PORT = 1080 ' default for SOCKS firewall servers.
' access types for InternetOpen()
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
' service types for InternetConnect()
Const INTERNET_SERVICE_URL = 0
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_SERVICE_GOPHER = 2
Const INTERNET_SERVICE_HTTP = 3
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 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 InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) 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 FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
(ByVal hConnect As Long, ByVal lpszSearchFile As String, _
ByRef lpvFindData As WIN32_FIND_DATA, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
' utilisé après FtpFindFirstFile pour continuer énumération
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
(ByVal hFind As Long, ByRef lpvFindData As WIN32_FIND_DATA) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" _
(ByVal hConnect As Long, ByVal lpszExisting As String, _
ByVal lpszNew As String) As Boolean
' ------------------------------------------------------
' Renommer avec wildcards
'
' Entrées
' Nom : Nom à renommer
' MasqueEntree : Masque entrée ex: toto*.txt
' MasqueSortie : Masque sortie ex: titi*.txt
'
' Sortie : String
' Nouveau Nom, ou ancien Nom s'il ne respecte pas le masque d'entrée
' ------------------------------------------------------
Function NvNom(Nom As String, _
ByVal MasqueEntree As String, _
ByVal MasqueSortie As String) As String
Dim oRegEx As RegExp ' Référence : Microsoft VBScript Regular Expression 5.5
Dim strRplc As String, i As Integer, c As String, itm As Integer
MasqueEntree = Replace(MasqueEntree, "\", "\\")
MasqueEntree = Replace(MasqueEntree, ".", "\.")
MasqueEntree = Replace(MasqueEntree, "*", "(.*)")
MasqueEntree = Replace(MasqueEntree, "?", "(.)")
' String for .Replace(...,...)
itm = 1: strRplc = ""
For i = 1 To Len(MasqueSortie)
c = Mid(MasqueSortie, i, 1)
Select Case c
Case "*", "?"
strRplc = strRplc & Format(itm, "\$#")
itm = itm + 1
Case Else
strRplc = strRplc & c
End Select
Next
Set oRegEx = New RegExp
oRegEx.IgnoreCase = True
oRegEx.Pattern = MasqueEntree
NvNom = oRegEx.Replace(Nom, strRplc)
End Function
' ------------------------------------------------------
' Renommer des fichiers sur serveur ftp avec wildcards
'
' Entrées
' strFTPsvr : nom du serveur ftp
' UID : nom utilisateur ftp
' Pwd : mot de passe
' DossierFTP : Dossier sur le serveur ftp
' FichierFTP : fichier(s) recherché(s)
' nom de fichier ou masque (toto*.txt)
' RenommerEn : nouveau nom de fichier ou masque (titi*.txt)
' Le nombre de * et ? ne doit pas être supérieur à
' celui des fichiers recherchés
'
' Sortie : aucune
' ------------------------------------------------------
Sub RenameFtpFiles(strFTPsvr As String, UID As String, Pwd As String, _
DossierFTP As String, FichierFTP As String, _
RenommerEn As String)
' Internet Handles for Internet Session, Internet Connection, Internet File
Dim hInternetSess As Long, hIConnect As Long, hIFile As Long
' Internet handle for FtpFindFirstFile, and Find File Structure
Dim hIFF As Long, ffF As WIN32_FIND_DATA
'
Dim RetVal As Long, fileName As String, p As Long
Dim fileNameNew As String
'Ouvre session internet
hInternetSess = InternetOpen("MonAppli", INTERNET_OPEN_TYPE_PRECONFIG, _
vbNullString, vbNullString, 0)
'Connection au serveur ftp
hIConnect = InternetConnect(hInternetSess, strFTPsvr, _
INTERNET_DEFAULT_FTP_PORT, UID, Pwd, _
INTERNET_SERVICE_FTP, 0, 0)
' Définit le répertoire distant
RetVal = FtpSetCurrentDirectory(hIConnect, DossierFTP)
' Enumération fichier(s)
ffF.cFileName = String(MAX_PATH, vbNullChar)
hIFF = FtpFindFirstFile(hIConnect, FichierFTP & vbNullChar, ffF, 0, 0)
If hIFF <> 0 Then
Do
p = InStr(1, ffF.cFileName, vbNullChar)
fileName = Left(ffF.cFileName, p - 1)
fileNameNew = NvNom(fileName, FichierFTP, RenommerEn)
#If DEBUG_ON = False Then
' renomme fichier Ftp
FtpRenameFile hIConnect, fileName, fileNameNew
#Else
Debug.Print fileName, fileNameNew
#End If
If InternetFindNextFile(hIFF, ffF) = False Then Exit Do
Loop
InternetCloseHandle (hIFF)
End If
InternetCloseHandle hIConnect 'Ferme la connection
InternetCloseHandle hInternetSess 'Ferme la session internet
End Sub |
Partager