Torgeir
Mon Feb 21 12:30:28 CST 2005
Sandra wrote:
> Hi,
>
> I'm looking for a script that:
> deletes *.xxx files that are older then Y days.
> I have seen a couple of scripts that delete 'old' files in a specific
> folder, but my script should only delete a very specific extention in a
> specific folder + all subfolders.
> Example:It should be capable of deleting all *.ver files located in c:\test
> \ and all subfolders, only if they are older then 14 days.
Hi
Below is a script doing this (based on a script by Michael Harris that
deletes files that has not been modified in x days old in a specific
folder and optionally all subfolders, original script here:
http://groups.google.co.uk/groups?selm=3DA1A19B.DF9B06D7%40hydro.com
)
'--------------------8<----------------------
'If bIncludeSubFolders is set to True, it will include subfolders
' folder to start search in...
path = "c:\temp"
' delete files with this extension...
killextension = "ver"
' delete files older than 14 days...
killdate = date() - 14
arFiles = Array()
set fso = createobject("scripting.filesystemobject")
' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true
nDeleted = 0
for n = 0 to ubound(arFiles)
'=================================================
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
'=================================================
on error resume next 'in case of 'in use' files...
arFiles(n).delete true
if err.number <> 0 then
wscript.echo "Unable to delete: " & arFiles(n).path
else
nDeleted = nDeleted + 1
end if
on error goto 0
next
msgbox nDeleted & " of " & ubound(arFiles)+1 _
& " eligible files were deleted"
sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
on error resume next
'select files to delete and add to array...
'
set folder = fso.getfolder(sPath)
set files = folder.files
for each file in files
' uses error trapping around access to the
' Date property just to be safe
'
dtlastmodified = null
on error resume next
dtlastmodified = file.datelastmodified
on error goto 0
if not isnull(dtlastmodified) then
if dtlastmodified < vKillDate and _
lcase(fso.getextensionname(file)) = lcase(killExtension) Then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
end if
end if
next
if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
next
end if
end sub
'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx