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
| Sub ChangerNoms()
Dim i As Long, nPos As Long
Dim Dic As New Collection
Dim nName As String, nNew As String, Ext As String
Dim oDossier, oFile, fso As Object
Dim ColonneSource, nNdr As Long
oDossier = "C:\Users\pc\Desktop\fld"
Do
ColonneSource = Application.InputBox("Entrer la colonne source", Type:=2) 'Demande le nom de la colonne source
If VarType(ColonneSource) = vbBoolean Then 'si rien n'a été saisi l'opération est annulée
Exit Sub
End If
If UCase(ColonneSource) Like "[BCD]" Then Exit Do
Loop
With Worksheets("Feuil1")
For i = 2 To .Cells(Rows.Count, "A").End(xlUp).Row 'Place les noms dans colonne A dans le dico
nName = Left(.Cells(i, "A"), 7)
Dic.Add i, UCase(nName)
Next
Set fso = CreateObject("Scripting.Filesystemobject")
For Each oFile In fso.GetFolder(oDossier).Files
nName = fso.GetBaseName(oFile.Name)
nName = Left$(nName, 7)
On Error Resume Next
nPos = Dic(UCase(nName)) 'vérifie la présence du nom du fichier dans le dico
If Err.Number = 0 Then 'Nom trouvé
Ext = fso.GetExtensionName(oFile.Name)
If Ext <> "" Then: Ext = "." & Ext
nNew = oFile.ParentFolder & "\" & .Cells(nPos, ColonneSource) & Ext
oFile.Move nNew 'renommer le fichier
nNdr = nNdr + 1
Else
Err.Clear
End If
On Error GoTo 0
Next
End With
MsgBox nNdr & " Fichier mis à jour"
End Sub |
Partager