Effacer fichier de plus de x jours

cs_vyper55 Messages postés 3 Date d'inscription mercredi 22 mars 2006 Statut Membre Dernière intervention 29 janvier 2007 - 29 janv. 2007 à 15:27
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 30 janv. 2007 à 06:05
je suis débutant en vbs,
j'ai recupérer ce script qui permet de nettoyer les fichiers de plus de x jours avec une interface graphique

le but de lancer ce programme tout les x jour de la semaine ou du moi sur un serveur et ainsi nettoyer des répertoires des utilisateurs

donc le lancer au travers d'un batch en automatique via le planificateur de tâches
et pour cela je souhaiterai retirer l'interface graphique du programme afin de le lancer comme un batch sans intervention d'un utilisateur ou d'un admin system

merci ci-dessous le script

'*******************************************************************************
' 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)>90 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)>90 then
            'Wscript.Echo objFile.Name & chr (10) &  objReadOnlyFile.DateLastModified
            objFile.delete


          end if


        Next


        GetSubFolders strFolderName
    Next
End Sub


Set FSO = CreateObject("Scripting.FileSystemObject")


       set folder = FSO.GetFolder (path)'utilisation du FSO pour prendre le dossier
       For Each B in folder.subfolders 'pour chaque dossier dans le dossier racine
                If B.size = 0 Then 'si la taille du dossier est egal a 0 (donc vide)
                 'msgbox B & B.size 'effacer le dossier
                 FSO.DeleteFolder B, force = yes
                End If 'fin du SI
               
                Next

8 réponses

jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
29 janv. 2007 à 15:31
Salut,

Et quel est ton problème exactement?

@+: Ju£i?n
Pensez: Réponse acceptée
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
29 janv. 2007 à 15:32
Et alors ou est le problème ?

Ce code ne fait apparement pas appel à une interface graphique, donc tu ne devrais pas vaoir de mal à t'en passer.

---- Sevyc64  (alias Casy) ----<hr size="2" width="100%" /># LE PARTAGE EST NOTRE FORCE #
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
29 janv. 2007 à 15:50
 Bonjour à tous

Bonjour, Julien et Casy,
Il faut soit le tester, ce script,....ou deviner quelques questions que se pose(nt) peut-être vyper55 .

pour le x, nombre de jours, c'est:

if DateDiff("d",objReadOnlyFile.DateLastModified ,Date)>90 then
     objFile.delete
end if

Je n'ai pas vu cité(copié) l'objet "objWbemLocator".

jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
29 janv. 2007 à 16:00
 Re,

Attention:
Quand vous proposez un code (ou bout), il faut toujours protéger les actions néfastes du code.
En l'occurrence, ici:
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)>90 then

'''''''''''''      objFile.delete
end if
Next

Et surtout, ne jamais tester un tel code !!!! ne pas oublier que :
strComputer = "."     c'est soi-même.

jean-marc
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_vyper55 Messages postés 3 Date d'inscription mercredi 22 mars 2006 Statut Membre Dernière intervention 29 janvier 2007
29 janv. 2007 à 16:13
bonjour jean-marc

merci de la réponse,

il s'agit d'un script que j'ai récupéré et fonctionne avec une interface grpahique l'utilisateur est invité à choisir au travers d'une interface graphique le repertoire racine.
0
cs_vyper55 Messages postés 3 Date d'inscription mercredi 22 mars 2006 Statut Membre Dernière intervention 29 janvier 2007
29 janv. 2007 à 16:21
ce script marche tres bien avec l'interface graphique, seulement le probleme c'est que j'aimerai l'exploiter sans cette interface et a ce niveau je suis archi débutant en vbs mon domaine c'est le reseau, linux et windows serveur (sytem et reseau, droit d'acces, web iis apache).
et donc en developpement vbs je suis nul.

merci de ta réponse.
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
29 janv. 2007 à 18:26
salut,
vyper55 -> le bouton "réponse acceptée" sert à marquer la ou les réponse qui solutionne(nt) ton problème.
si tu valides tout, plus personne ne viendra pour essayer de le résoudre...
corrigé

et évite les topics en doublon stp (je suppose que c'était involontaire)
supprimé

tous les autres -> le doublon est supprimé mais j'ai conservé au bloc-note, donc si une de vos réponses est à remettre ici... profitez avant la fin du monde ou une coupure de courant ^^

++
PCPT  [AFCK]
<hr size="2" width="100%" />Prenez un instant pour répondre à [infomsg_SONDAGE-POP3-POUR-CS_769706.aspx ce sondage] svp
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
30 janv. 2007 à 06:05
 Bonjour à tous

726766 vyper55 , j'ai adapté "ton" code pour qu'il puisse afficher les fichiers non modifiés depuis 15 jours dans un
répertoire. J'ai enlevé la gestion des sous-répertoires, ainsi que l'objet Shell.

Dans l'exemple ci-dessous, il suffit de modifier:

strComputer = "."
Const MyPath = "D:\Mes_scripts_VBFrance\Test"



Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")



strFolderName = MyPath



Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")



Wscript.Echo "vérif strFolderName : " &vbCrLf& strFolderName



arrFolderPath = Split(strFolderName, "")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
    strNewPath = strNewPath & "\" & arrFolderPath(i)
    Wscript.Echo "vérif strNewPath : " &vbCrLf& strNewPath
Next
strPath = strNewPath & "\"
Wscript.Echo "vérif strPath : " &vbCrLf& strPath



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 "vérif suppression fichier (date>15) " & vbCrLf& _
                 objFile.Name & chr (10) &  objReadOnlyFile.DateLastModified
     ''''''''''''''''''''''objFile.delete
    end if
Next



Set objWMIService = Nothing
Set objFSO = Nothing
Set colSubfolders = Nothing

Set colFiles = Nothing
Set objReadOnlyFile = Nothing





jean-marc
0
Rejoignez-nous