IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBScript Discussion :

Effaces fichier et dossier qui date de plus de 15Jr


Sujet :

VBScript

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 25
    Par défaut Effaces fichier et dossier qui date de plus de 15Jr
    Bonjour a tous je cherche a cree un script VBS pour Effaces les fichier et S/dossier qui date de plus de 15Jr dans un repertoire donné

    jai trouvé un script qui se rapproche de ce que je veut mais il est aleatoire et n'efface pas les S/dossier et les fichier dans les S/dossier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    '****************************************************************
    ' Script permettant D 'effacer les fichiers Qui date de plus de 15 jours
    ' Avec interface Graphique
    '
    '*****************************************************************
     
    strComputer = "."
     
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2" )
    Set objFSO = CreateObject("Scripting.FileSystemObject" )
    Set objShell = CreateObject("Shell.Application" )        
    Set objFolder = objShell.BrowseForFolder _
           (WINDOW_HANDLE, "Selectionner le dossier à traiter :", NO_OPTIONS, "." )
    Set objFolderItem = objFolder.Self
    strFolderName = objFolderItem.Path
     
    Set colSubfolders = objWMIService.ExecQuery _
           ("Associators of {Win32_Directory.Name='" & strFolderName & "'} "_
           & "Where AssocClass = Win32_Subdirectory " _
           & "ResultRole = PartComponent" )
     
    'Wscript.Echo strFolderName
     
    arrFolderPath = Split(strFolderName, "\" )
    strNewPath = ""
    For i = 1 to Ubound(arrFolderPath)
      strNewPath = strNewPath & "\\" & arrFolderPath(i)
    Next
    strPath = strNewPath & "\\"
     
    Set colFiles = objWMIService.ExecQuery _
           ("Select * from CIM_DataFile where Path = '" & strPath & "'" )
     
    For Each objFile in colFiles
      Set objReadOnlyFile = objFSO.GetFile(objFile.Name)
      'Wscript.Echo objFile.Name & chr (10) & objReadOnlyFile.DateLastModified
      if DateDiff("d",objReadOnlyFile.DateLastModified ,Date)>15 then
     
        objFile.delete
      end if
    Next
     
    For Each objFolder in colSubfolders
      GetSubFolders strFolderName
    Next
     
    Sub GetSubFolders(strFolderName)
      Set colSubfolders2 = objWMIService.ExecQuery _
             ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
             & "Where AssocClass = Win32_Subdirectory " _
             & "ResultRole = PartComponent" )
     
      For Each objFolder2 in colSubfolders2
        strFolderName = objFolder2.Name
        'Wscript.Echo
        'Wscript.Echo objFolder2.Name
        arrFolderPath = Split(strFolderName, "\" )
        strNewPath = ""
        For i = 1 to Ubound(arrFolderPath)
          strNewPath = strNewPath & "\" & arrFolderPath(i)
        Next
        strPath = strNewPath & "\"
     
        Set colFiles = objWMIService.ExecQuery _
                ("Select * from CIM_DataFile where Path = '" & strPath & "'" )
     
        For Each objFile in colFiles
          Set objReadOnlyFile = objFSO.GetFile(objFile.Name)
          if DateDiff("d",objReadOnlyFile.DateLastModified ,Date)>15 then
            'Wscript.Echo objFile.Name & chr (10) & objReadOnlyFile.DateLastModified
            objFile.delete
     
          end if
     
        Next
     
        GetSubFolders strFolderName
      Next
    End Sub
    merci d'avance de votre aide je suis un pauvre debutant qui cale rien ^^

  2. #2
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Par défaut
    Regarde ici : http://www.activexperts.com/activmon.../filesfolders/
    Cela pourait t'aider

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 25
    Par défaut Sa avance merci !!
    merci pour ta reponce j'ai pu avancer dans le chmilbik mais ! (il y a toujour un mais )
    lors de mon debugage une erreur me bloque tout


    68 For Each objFile in colFiles

    erreur Vb script l'execution na pas pu s'effectuer
    jai remarque que cela se produit dans dans mon dossier cible il y a des repertoires

    bizzare je pense que dans le 2m sub il n'arrive pas a detruire les dossier

    merci d'avance

  4. #4
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Par défaut
    Si tu veux que je puisse t'aider, il va falloir me fournir ton nouveau code, parce que la avec l'ancien, tu ne detruis aucun repertoires, juste des fichiers.
    En général lorsque tu as une erreur sur un "For Each objet In CollectionObjet" c'est que il y a eu un problème dans la création de la collection de l'objet. Dans ton cas, il y a peut être une erreur dans la requête SQL.

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 25
    Par défaut ^^
    voila je nais pas toucher le code mais seulement essayer de le comprendre a mon niveau c deja dur

    mais peut etre est 'il possible de le simplifier ?

    juste supprimer ce qui est plus vieux > 15Jrs fichier est dossier


    del *.*?

  6. #6
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Par défaut
    Regarde ici : http://msdn.microsoft.com/library/de...m_datafile.asp
    Cela t'explique ce qu'est l'objet Cim_DataFile et donc le methodes que tu peut utiliser.
    Si tu navigues dans l'arborescence sur la gauche, tu auras des explications sur ce que sont les objets WMI et une liste de ces objets, avec pour chacun une page explicative.

  7. #7
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 25
    Par défaut sa marche ^^
    voila jai trouver le code complet ailleurs et il marche impec

    je vous en donne une copie au cas ou sa interese qqn

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
        '*******************************************************************************
        ' Script permettant D 'effacer les fichiers Qui date de plus de 15 jours
        ' Avec interface Graphique
        '
        '*******************************************************************************
     
     
        strComputer = "."
     
       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set objShell = CreateObject("Shell.Application")
       Set objFolder = objShell.BrowseForFolder _
           (WINDOW_HANDLE, "Selectionner le dossier à traiter :", NO_OPTIONS, ".")
       Set objFolderItem = objFolder.Self
       strFolderName = objFolderItem.Path
     
       Set colSubfolders = objWMIService.ExecQuery _
           ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
               & "Where AssocClass = Win32_Subdirectory " _
                   & "ResultRole = PartComponent")
     
       'Wscript.Echo strFolderName
     
       arrFolderPath = Split(strFolderName, "\")
       strNewPath = ""
       For i = 1 to Ubound(arrFolderPath)
           strNewPath = strNewPath & "\\" & arrFolderPath(i)
       Next
       strPath = strNewPath & "\\"
     
       Set colFiles = objWMIService.ExecQuery _
           ("Select * from CIM_DataFile where Path = '" & strPath & "'")
     
       For Each objFile in colFiles
           Set objReadOnlyFile = objFSO.GetFile(objFile.Name)
            'Wscript.Echo objFile.Name & chr (10) & objReadOnlyFile.DateLastModified
        if DateDiff("d",objReadOnlyFile.DateLastModified ,Date)>1 then
     
           objFile.delete
       end if
       Next
     
       For Each objFolder in colSubfolders
           GetSubFolders strFolderName
       Next
     
       Sub GetSubFolders(strFolderName)
           Set colSubfolders2 = objWMIService.ExecQuery _
               ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
                   & "Where AssocClass = Win32_Subdirectory " _
                       & "ResultRole = PartComponent")
     
           For Each objFolder2 in colSubfolders2
               strFolderName = objFolder2.Name
               'Wscript.Echo
               'Wscript.Echo objFolder2.Name
               arrFolderPath = Split(strFolderName, "\")
               strNewPath = ""
               For i = 1 to Ubound(arrFolderPath)
                   strNewPath = strNewPath & "\\" & arrFolderPath(i)
               Next
               strPath = strNewPath & "\\"
     
               Set colFiles = objWMIService.ExecQuery _
                   ("Select * from CIM_DataFile where Path = '" & strPath & "'")
     
               For Each objFile in colFiles
               Set objReadOnlyFile = objFSO.GetFile(objFile.Name)
               if DateDiff("d",objReadOnlyFile.DateLastModified ,Date)> 1 then
                   'Wscript.Echo objFile.Name & chr (10) & objReadOnlyFile.DateLastModified
                   objFile.delete
     
                 end if
     
               Next
     
               GetSubFolders strFolderName
           Next
       End Sub

  8. #8
    Expert confirmé
    Avatar de ced600
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Août 2006
    Messages
    3 364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Août 2006
    Messages : 3 364
    Par défaut
    Où est la différence avec le code que tu as mis dans ton premier message ?
    Ce code n'est pas près de t'effacer un répertoire, ou je ne sais par quel miracle, puisqu'il n'y a pas de truc comme deletefolder !!!

  9. #9
    Membre averti
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 25
    Par défaut ^^
    je n'ais pas lu tout le code j'etait trop content quil s'exectute tout bien que jai pas regarder pour les dossier --> noob ^^

    je vias rajouter un bout de script qui efface tous les dossier vides jai deja commencer

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ShowSubFolders FSO.GetFolder("d:\")
     
    For Each Subfolder in Folder.SubFolders
           ShowSubFolders Folder
       Next
     
    Sub ShowSubFolders(Folder)
     
        For Each Subfolder in Folder.SubFolders
            If Subfolder.Size = 0 Then
            objFSO.DeleteFolder Subfolder
            End If
            ShowSubFolders Subfolder
        Next
    End Sub
    merci beaucoup pour ton aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Batch] classer fichier dans dossier selon date de création
    Par g0g059 dans le forum Scripts/Batch
    Réponses: 1
    Dernier message: 13/03/2012, 21h31
  2. Réponses: 1
    Dernier message: 14/10/2011, 15h33
  3. lftp - Effacer fichiers et dossier recursivement
    Par Damouille dans le forum Linux
    Réponses: 1
    Dernier message: 04/09/2009, 12h28
  4. Réponses: 2
    Dernier message: 13/07/2008, 14h57
  5. Effacer - Fichiers - Proteger - Dossier
    Par imparator_42 dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 30/03/2008, 17h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo