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
| Option Explicit
Const ForWriting = 2, ForReading = 1, BIF_RETURNONLYFSDIRS = 1
Const MSG = "Parcours d'un dossier et renommage de ses fichiers"
Const sName = "Bonjour_"
Dim oFSO, Idx, F, Verif, sPath, sFile,WS
Set oFSO = CreateObject("Scripting.FileSystemObject")
sPath = oFSo.GetFolder(SelectFolder).Path
sFile = sPath & "\Scanned.txt"
Set Verif = oFSO.OpenTextFile(sFile, ForReading, True)
Verif.Close
'Idx = 1
' On vérifie la taille d'un fichier que j'appelerais 'témoin' du renommage
' dont la taille devrait faire 60 octets.
If oFSO.GetFile(sFile).Size > 50 Then
MsgBox "Le Dossier " & UCase(sPath) & " a été parcouru." & _
vbCrLf & "Oprération effectuée : " & oFSO.OpenTextFile(sFile, ForReading).ReadAll & _
vbCrLf & "On quitte le script sans rien modifier" ,vbInformation
Wscript.Quit 0
Else
ScanFolders sPath
MsgBox "Opération terminée."
End If
Sub ScanFolders(oFolder)
Dim oSubFolder
Idx = 1
For Each F In oFSO.GetFolder(oFolder).Files
If Err.Number <> 0 Then
Err.Clear
Else
If F.Name <> "" And oFSo.GetExtensionName(F.Name) <> "" Then
F.Name = sName & Right("000" & CStr(Idx), 3) & "." & oFSo.GetExtensionName(F.Name)
Idx = Idx + 1
End If
End If
Next
For Each oSubFolder In oFSO.GetFolder(oFolder).SubFolders
Idx = 1
If Err.Number <> 0 Then
Err.Clear
Else
ScanFolders oSubFolder
End If
Next
Set Verif = oFSO.OpenTextFile(sFile, ForWriting, True)
Verif.Write "Dossier parcouru et fichiers renommés le " & Now
Verif.Close
End Sub
'==============================
Function SelectFolder()
Dim WSH, Item, lngFlag, Result, InitDir, DialogTitle, drv, fso
Set WSH = WScript.CreateObject("Shell.Application")
lngFlag=BIF_returnonlyfsdirs
InitDir = "C:"
DialogTitle="Sélection de dossier :(Pas de Poste de travail, Favoris réseau et/ou ses sous-éléments)"
Set Item = WSH.BrowseForFolder(0,DialogTitle,lngFlag, "")
If Item Is Nothing Then WScript.Quit 0
Set WS = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If LCase(Item.Title) = "poste de travail" Or LCase(Item.Title) = "favoris réseau" Then
MsgBox "Mauvais choix de répertoire [" & UCase(Item.Title) & "]" & VbCrLf & _
"Choisir un répertoire valide puis réessayer."
WScript.Quit 0
End If
If LCase(Item.Title) = "mes documents" Or LCase(Item.Title) = "bureau" Then
Result = WS.ExpandEnvironmentStrings("%UserProfile%") & "\" & Item.Title
ElseIf Is_Value(Item) Then
Result = Item.Title
' A-t-on sélectionné la racine d'un lecteur ?
If Right(Result, 2) = ":)" Then
' Quand il s'agit de la racine d'un lecteur il faut tenir compte de son nom
' ( ou Label) qui se présente sous la forme NomLecteur(X:)
Set drv = fso.GetDrive(Left(Right(Result, 3), 2))
Result = drv.RootFolder
ElseIf InStr(1, Result, ":") = 0 Then
Result = Item.ParentFolder.ParseName(Item.Title).Path
End If
End If
SelectFolder = Result
End Function
'======================
Function Is_Value(obj)
Dim stmp
On Error Resume Next
stmp = " " & obj
Is_Value = (Err = 0)
On Error GoTo 0
End Function |
Partager