VBS, LISTER FICHIERS

cs_lilsgabbg Messages postés 50 Date d'inscription dimanche 24 décembre 2006 Statut Membre Dernière intervention 9 septembre 2009 - 7 nov. 2007 à 19:28
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 8 nov. 2007 à 20:50
Bonjour à tous, je veux créer un script qui, si le disque dur C et le D existe, liste tous les fichiers se trouvant sur le Bureau, mon script ne marche pas, mais je ne vois pas où...

Merci de m'aider et bonne soirée ;)

Sub Ld D ()

Option Explicit

On Error Resume Next

if oFSO.DriveExists("D") then
Dim Wsh,Nom
Set Wsh = WScript.CreateObject("WScript.network")
Nom = Wsh.UserName
C ()
end if

end sub

sub C ()
On Error Resume Next
Dim oFso 'As FileSystemObject
Dim stRep
stRep = "c:\documents and settings" & Nom & "\Bureau" 'Répertoire à parcourir
Set oFso = CreateObject("Scripting.FileSystemObject")
listfiles oFso.GetFolder(stRep)
end sub

sub D ()
On Error Resume Next
Dim oFso 'As FileSystemObject
Dim stRep
stRep = "d:\documents and settings" & Nom & "\Bureau" 'Répertoire à parcourir
Set oFso = CreateObject("Scripting.FileSystemObject")
listfiles oFso.GetFolder(stRep)
end sub

Sub listfiles(rep )
 Dim f 'As File
 Dim r 'As Folder
 For Each f In rep.Files
   MsgBox f.Path & f.Name
 Next
 For Each r In rep.SubFolders 'Boucle dans sous répertoires
   listfiles r
 Next
End Sub

1 réponse

cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
8 nov. 2007 à 20:50
 Bonsoir,

Attention, il est plus aisé de translater du vbs en vb6 que l'inverse.
Ce n'est pas, via le bureau que l'on liste les fichiers.
Dans l'exemple ci-dessous, il y a parcours de tous les disks ntfs et création
sur le dektop d'un fichier .txt énumérant tous les fichiers.
D'autre part, j'entr'aperçois l'utlisation de l'objet Network à mauvais escient,
à remplacer par:
Set WshShell = WScript.CreateObject("WScript.Shell")
sUserProfil =  WshShell.Environment("PROCESS").Item("userprofile")
''''sPathLocal = WshShell.ExpandEnvironmentStrings("%WinDir%") & ""
WScript.Echo sUserProfil ''''&vbCrLf& sPathLocal
Set WshShell = Nothing

________________________________________
'
'Enumération des fichiers sur disks NTFS
'
Option Explicit

Dim objFso, objTxt, objOutFile, WshShell, objDrive, i
Set objFso = Createobject("Scripting.FileSystemobject")      
Set WshShell = WScript.Createobject("WScript.Shell")

objOutFile = WshShell.SpecialFolders("Desktop") & _
                    "\liste_fichiers.txt"

Set objTxt = objFso.OpenTextFile(objOutFile, 2, True)

For Each objDrive in objFso.Drives     If objDrive.IsReady True And objDrive.DriveType 2 Then
       Dim objDicoFiles
       Set objDicoFiles = Createobject("Scripting.Dictionary")
   
       Call SearchFiles(objFso.GetFolder(objDrive.RootFolder))
 
       'MsgBox "Nombre fichiers" &vbTab& objDicoFiles.Count,, _
       '       "Disk " & objDrive.RootFolder
      
       Dim elements
       elements = objDicoFiles.Items
       'écriture dans le fichier txt      
       For i=0 To objDicoFiles.Count-1
           objTxt.WriteLine elements(i) 
       Next
       Set objDicoFiles = Nothing
    End if
Next
objTxt.Close

Dim Return
Return = WshShell.Run("notepad " & objOutFile, 1, True)

Set objTxt = Nothing
Set objFso = Nothing
Set WshShell = Nothing
'
WScript.Echo "Script " & Wscript.ScriptFullName & " terminé ...."
'
Sub SearchFiles(objFolder)
    On Error Resume Next
    Dim objFile, subFolder
    For Each objFile In objFolder.Files
        Set objDicoFiles(objFile.Path) = objFile
    Next
    For Each subFolder In objFolder.SubFolders
        Call SearchFiles(subFolder)
    Next
End Sub

jean-marc
0
Rejoignez-nous