Bonjour,

je dois purger (purges hebdomadaires) des fichiers et des dossiers avec des durées de retention de 6 jours et 30 jours.

Mon script fonctionne bien j'arrive a purger les fichiers, mais lorsqu'il s'agit de supprimer des sous dossiers ça ne passe pas ?
Je m'appuie sur un fichier.ini puge.ini

[PURGE]

listpurge=E:\projets\log;E:\CR\log;E:\projets\postprod\log;

retention=6 OK
[PURGE1]

Il me supprime bien les logs dans :E:\projets\log;E:\CR\log;E:\projets\postprod\log



listpurge=E:\projets\tmp; E:\projets\CR\tmp

retention=30 OK
[PURGE2]

Il me supprime bien les fichiers temp dans: E:\projets\tmp; E:\projets\CR\tmp


listpurge=E:\projets\undeliverable (il y a des sous-dossiers (non vides) que je veux supprimer)

retention=30 KO

Je n'arrive pas a supprimer les sous dossiers dans : undeliverable

Merci à tous


Mon script:


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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'*************
'   VARIABLES
'*************
	On Error Resume Next
 
	ScriptName = "Purge.vbs"
	PathINI = "C:\Outil\Batch\bin\purge.ini"
 
	Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
 
 
'*************
'   MAIN 
'*************
 
	If (FileExists (PathINI) = "1") Then
			wscript.echo "Fichier de paramétrage manquant"
			WScript.Quit
 
	End If
 
	section = "PURGE"
	incr = 0
 
	Do While (ini_testSection (PathINI, section) = "1")
 
		list_purge = ini_readValue (PathINI, section, "listpurge")
		ret = ini_readValue (PathINI, section, "retention")
 
 
		Do While list_purge <> "" 
			pos = Instr (list_purge, ";")
			scr = ""
			Select Case pos
				case "0"
					scr = Trim (list_purge)
					list_purge = ""
				case "1"
					list_purge = Trim (Mid (list_purge, pos+1))
				case else
					scr = Trim (Left (list_purge, pos-1))
					list_purge = Trim (Mid (list_purge, pos+1))
			End Select	
 
			If (PathExists (scr) = "0") Then
					Set Afolder=FSO.GetFolder(scr)
					Set TheFiles = Afolder.Files
 
					For Each AFile In TheFiles
						If DateDiff("d",(FSO.GetFile(AFile)).DateLastAccessed,Date) > Int(ret) Then
							Name = AFile.Name
							AFile.Delete
							log_writeLine AFolder & Name & " supprimé"
						End If
					Next	
				ElseIf FileExists(scr) = "0" Then
					Set Afile=FSO.GetFile(scr)
 
					If DateDiff("d",Afile.DateLastAccessed,Date) > Int(ret_delete) Then
						Name = Afile.Name
						Afile.Delete
						log_writeLine Name & " supprimé"
					End If
				Else
					log_writeLine "Le Dossier ou fichier à purger n'existe pas : " & scr
			End If
		Loop
 
 
		incr = incr+1
		section = section & incr
	Loop
 
	If Err <> 0 Then
        log_writeLine "ERREUR: "& Err.Number &" "&  Err.Description &""
		Err.Clear
	End If
 
	log_writeLine "Fin du script " & ScriptName & ""
 
 
 
'*************
'   FUNCTIONS
'*************
 
 
' --------------------------------------------------------------------
' Descriptif : Test l'existence d'un répertoire
' Entrée	 : Pathname = Chemin du répertoire
'            
' Sortie	 : retourne 0 si le répertoire existe ou 1 
'---------------------------------------------------------------------
Function PathExists(Pathname)
	Set oFs = CreateObject("Scripting.FileSystemObject")
 
	If oFs.FolderExists(Pathname) = False Then
		PathExists = 1 
	Else
		PathExists = 0
	End If
	Set oFs = Nothing
End Function 
 
' --------------------------------------------------------------------
' Descriptif : Test l'existence d'un fichier
' Entrée	 : Fname = Chemin du fichier
'            
' Sortie	 : retourne 0 si le fichier existe ou 1 
'---------------------------------------------------------------------
Function FileExists(Fname)
	Set oFs = CreateObject("Scripting.FileSystemObject")
 
	If oFs.FileExists(Fname) = False Then
		FileExists = 1
	Else
		FileExists = 0
	End If
	Set oFs = Nothing
End Function 
 
 
' --------------------------------------------------------------------
' Descriptif : Recherche une chaine dans un fichier ini
' Entrée	 : strFile  = Chemin du fichier ini
'              strSection   = Section de la variable
'              strVariable = Variable
' Sortie	 : Retourne la valeur de la Variable si trouvé, sinon default
'---------------------------------------------------------------------	
Function ini_readValue(strFile, strSection, strVariable)
	Set oFs = WScript.CreateObject("Scripting.FileSystemObject")
	Dim strLine, ini, p, ustrSection, ustrVariable
	'ReadProfileString = strDefault
	ustrSection = UCase(strSection) : ustrVariable = UCase(strVariable)
 
		If oFs.FileExists( strFile ) Then
		Set ini = oFs.OpenTextFile( strFile, 1, False)
		Do While ini.AtEndOfStream = False
			strLine = Trim(ini.ReadLine)
			If( "[" & ustrSection & "]" = UCase(strLine) ) Then
				strLine = Trim(ini.ReadLine)
				Do While Left(strLine, 1) <> "["
					p = InStr(strLine, "=")
					If p > 0 Then
						If( UCase(RTrim(Left(strline, p-1))) = ustrVariable ) Then
							ini_readValue = LTrim(Mid(strLine, p+1))
							'ReadProfileString = LTrim(Mid(strLine, p+1))
							Exit Do
						End If
					End If
					If ini.AtEndOfStream Then Exit Do
					strLine = Trim(ini.ReadLine)
				Loop
				Exit Do
			End If
		Loop
		ini.Close
	End If                                  'strFile exists
End Function  
 
' --------------------------------------------------------------------
' Descriptif : Vérifie l'existance d'une section dans un fichier ini
' Entrée	 : strFile  = Chemin du fichier ini
'              strSection   = Section de la variable
'              
' Sortie	 : Retourne 1 si la section existe, sinon 0
'---------------------------------------------------------------------
Function ini_testSection (strFile, strSection)
	Set oFs = WScript.CreateObject("Scripting.FileSystemObject")
	Dim strLine, ini, ustrSection
	ustrSection = UCase(strSection)
	ini_testSection =  "0"
 
	If oFs.FileExists( strFile ) Then
		Set ini = oFs.OpenTextFile( strFile, 1, False)
		Do While ini.AtEndOfStream = False
			strLine = Trim(ini.ReadLine)
			If( "[" & ustrSection & "]" = UCase(strLine) ) Then
				ini_testSection =  "1"
			End If
		Loop
		ini.Close
	End If                             'strFile exists
	Set oFs = Nothing 
End Function
 
' --------------------------------------------------------------------
' Descriptif : Ecriture d'une ligne dans la LOG
' Entrée	 : Message = Description 
'            
' Sortie	 : - 
'---------------------------------------------------------------------
Function log_writeLine(Message)
 
	Dim Fichier_Log
	Dim Computer
 
	Set network = Wscript.CreateObject("WScript.Network")
	Computer = network.ComputerName
	Fichier_Log = "C:\Outil\Batch\bin\purge.log"
	Set oFicLog = FSO.OpenTextFile(Fichier_Log, 8, True)
 
	oFicLog.WriteLine Now & " - " & Message
 
	oFicLog.Close
 
	Set oFicLog = Nothing
 
End Function
Mon fichier purge.ini

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
[PURGE]
 
listpurge=E:\projets\log;E:\CR\log;E:\projets\postprod\log;
 
retention=6
 
[PURGE1]
 
listpurge=E:\projets\tmp; E:\projets\CR\tmp
 
retention=30
 
[PURGE2]
 
listpurge=E:\projets\undeliverable
 
retention=30