PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.win.server.scripting > Need with delete old files script
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Need with delete old files script

Réponse
 
LinkBack Outils de la discussion
Vieux 08/10/2007, 18h56   #1
Dave Allen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Need with delete old files script

I need a script that will delete files and folders older then 30 days.
I found this script and it works except it does not scan sub-folders

http://blogs.msdn.com/benjguin/archi...es-script.aspx

Does anyone know how to make this scan sub-folders?


cscript DeleteOldFiles.vbs "C:\Test" 30


option explicit

Call DoTheJob()
WScript.Echo "--- end of script execution ---"

Sub DoTheJob
dim limitDate
dim formattedLimitDate
dim folder
dim strComputer
dim objWMIService
dim colFileList
dim objFile
dim nbFiles
dim totalFiles
dim nbErrors
dim result
dim nbDays

if WScript.Arguments.Count <> 2 then
WScript.Echo "usage : DeleteOldFiles.vbs <folder> <nb of days ago>"
WScript.Echo "sample: DeleteOldFiles.vbs C:\Windows\temp 90"
Exit Sub
end if

folder = WScript.Arguments(0)
nbDays = WScript.Arguments(1)

'calculate and format limit date
limitDate = DateAdd("d", -1 * nbDays , Date)

formattedLimitDate = DatePart("yyyy", limitDate)

if DatePart("m", limitDate) < 10 then
formattedLimitDate = formattedLimitDate & "0"
end if
formattedLimitDate = formattedLimitDate & DatePart("m", limitDate)

if DatePart("d", limitDate) < 10 then
formattedLimitDate = formattedLimitDate & "0"
end if
formattedLimitDate = formattedLimitDate & DatePart("d", limitDate)

'show what will be done
WScript.Echo "Will remove files from " & folder & " with a date older
than " & formattedLimitDate & " (" & nbDays & " days ago)"


'Get the files and delete the old ones
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & folder & "'} Where " _
& "ResultClass = CIM_DataFile")

nbFiles = 0
totalFiles = 0
nbErrors = 0

For Each objFile In colFileList
totalFiles = totalFiles + 1
if objFile.CreationDate < formattedLimitDate then

result = objFile.Delete()

WScript.Echo objFile.Name & " - " & objFile.CreationDate & ".
Delete Result: " & result
if result = 0 then
nbFiles = nbFiles + 1
else
nbErrors = nbErrors + 1
end if
end if
Next

'Show the result
Wscript.Echo "Total files in folder: " & totalFiles
WScript.Echo "Deleted files: " & nbFiles
WScript.echo "Errors: " & nbErrors
End Sub



  Réponse avec citation
Vieux 09/10/2007, 19h56   #2
dveit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Need with delete old files script

Try building a WMI query or WSH FSO function that enumerates the subfolders
from the root folder you're checking. Then for each subfolder run your
routine that does the file cleanup. Plagiarized from the Windows 2000
Scripting Guide:

strRootFolder = "c:\test"
strComputer = "."
Set objWMIService = GetObject("winmgmts:"_
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strRootFolder & "'} " _
& "WHERE AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

For Each objFolder in colSubFolders
'Run your subroutine to purge old files here
Next

"Dave Allen" <nomail@namail.com> wrote in message
news:O3Vw0SdCIHA.4752@TK2MSFTNGP04.phx.gbl...
>I need a script that will delete files and folders older then 30 days.
> I found this script and it works except it does not scan sub-folders
>
> http://blogs.msdn.com/benjguin/archi...es-script.aspx
>
> Does anyone know how to make this scan sub-folders?
>
>
> cscript DeleteOldFiles.vbs "C:\Test" 30
>
>
> option explicit
>
> Call DoTheJob()
> WScript.Echo "--- end of script execution ---"
>
> Sub DoTheJob
> dim limitDate
> dim formattedLimitDate
> dim folder
> dim strComputer
> dim objWMIService
> dim colFileList
> dim objFile
> dim nbFiles
> dim totalFiles
> dim nbErrors
> dim result
> dim nbDays
>
> if WScript.Arguments.Count <> 2 then
> WScript.Echo "usage : DeleteOldFiles.vbs <folder> <nb of days ago>"
> WScript.Echo "sample: DeleteOldFiles.vbs C:\Windows\temp 90"
> Exit Sub
> end if
>
> folder = WScript.Arguments(0)
> nbDays = WScript.Arguments(1)
>
> 'calculate and format limit date
> limitDate = DateAdd("d", -1 * nbDays , Date)
>
> formattedLimitDate = DatePart("yyyy", limitDate)
>
> if DatePart("m", limitDate) < 10 then
> formattedLimitDate = formattedLimitDate & "0"
> end if
> formattedLimitDate = formattedLimitDate & DatePart("m", limitDate)
>
> if DatePart("d", limitDate) < 10 then
> formattedLimitDate = formattedLimitDate & "0"
> end if
> formattedLimitDate = formattedLimitDate & DatePart("d", limitDate)
>
> 'show what will be done
> WScript.Echo "Will remove files from " & folder & " with a date older
> than " & formattedLimitDate & " (" & nbDays & " days ago)"
>
>
> 'Get the files and delete the old ones
> strComputer = "."
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer &
> "\root\cimv2")
>
> Set colFileList = objWMIService.ExecQuery _
> ("ASSOCIATORS OF {Win32_Directory.Name='" & folder & "'} Where " _
> & "ResultClass = CIM_DataFile")
>
> nbFiles = 0
> totalFiles = 0
> nbErrors = 0
>
> For Each objFile In colFileList
> totalFiles = totalFiles + 1
> if objFile.CreationDate < formattedLimitDate then
>
> result = objFile.Delete()
>
> WScript.Echo objFile.Name & " - " & objFile.CreationDate & ".
> Delete Result: " & result
> if result = 0 then
> nbFiles = nbFiles + 1
> else
> nbErrors = nbErrors + 1
> end if
> end if
> Next
>
> 'Show the result
> Wscript.Echo "Total files in folder: " & totalFiles
> WScript.Echo "Deleted files: " & nbFiles
> WScript.echo "Errors: " & nbErrors
> End Sub
>
>
>



  Réponse avec citation
Vieux 09/10/2007, 21h47   #3
urkec
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Need with delete old files script

"Dave Allen" wrote:

> I need a script that will delete files and folders older then 30 days.
> I found this script and it works except it does not scan sub-folders
>
> http://blogs.msdn.com/benjguin/archi...es-script.aspx
>
> Does anyone know how to make this scan sub-folders?


I tried to modify your script to enumerate files recursively.
Based on a post I saw in this group recently, the script will not work with
folder names that include "'" or "}". In that case you will have to use
double instead of single quotes and double the backslashes when executing the
ASSOCIATORS OF query (I hope I got that right). If you are not going to use
the script remotely, FSO is faster than WMI (recent post by R. Mueller
compares the two methods).

This is the modified script, I used ASSOCIATORS OF query with
Win32_SubDirectory class, like in the post by Dveit. I don't have time to
test it, but if you search this group, you can find other samples for working
with files and folders.

option explicit

if WScript.Arguments.Count <> 2 then
WScript.Echo "usage : DeleteOldFiles.vbs <folder> <nb of days ago>"
WScript.Echo "sample: DeleteOldFiles.vbs C:\Windows\temp 90"
WScript.Quit
end if

dim folder
dim nbFiles
dim totalFiles
dim nbErrors
dim limitDate
dim formattedLimitDate
dim nbDays

nbFiles = 0
totalFiles = 0
nbErrors = 0

folder = WScript.Arguments(0)
nbDays = WScript.Arguments(1)

'calculate and format limit date
limitDate = DateAdd("d", -1 * nbDays , Date)

formattedLimitDate = DatePart("yyyy", limitDate)

if DatePart("m", limitDate) < 10 then
formattedLimitDate = formattedLimitDate & "0"
end if
formattedLimitDate = formattedLimitDate & DatePart("m", limitDate)

if DatePart("d", limitDate) < 10 then
formattedLimitDate = formattedLimitDate & "0"
end if
formattedLimitDate = formattedLimitDate & DatePart("d", limitDate)

'show what will be done
WScript.Echo "Will remove files from " & folder & " with a date older than "
& formattedLimitDate & " (" & nbDays & " days ago)"


Call DoTheJob(folder)

'Show the result
Wscript.Echo "Total files in folder: " & totalFiles
WScript.Echo "Deleted files: " & nbFiles
WScript.echo "Errors: " & nbErrors

WScript.Echo "--- end of script execution ---"


Sub DoTheJob (fldr)

dim strComputer
dim objWMIService
dim colFileList
dim objFile
dim result
dim colSubfolders
dim objSubfolder

'Get the files and delete the old ones
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & fldr & "'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFileList
totalFiles = totalFiles + 1
if objFile.CreationDate < formattedLimitDate then

'result = objFile.Delete()
WScript.Echo objFile.Name & " will be deleted"

'WScript.Echo objFile.Name & " - " & objFile.CreationDate & ".Delete Result:
" & result
if result = 0 then
nbFiles = nbFiles + 1
else
nbErrors = nbErrors + 1
end if
end if
Next

Set colSubfolders = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & fldr & "'} Where " _
& "AssocClass = Win32_SubDirectory " _
& "ResultRole = PartComponent")

For Each objSubfolder In colSubfolders
Call DoTheJob (objSubfolder.Name)
Next

End Sub


--
urkec
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12095 seconds with 11 queries