Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looking for script to delete files older than X days

69 views
Skip to first unread message

Jim

unread,
Aug 20, 2002, 12:10:59 PM8/20/02
to
Anyone know of a script that will recursively delete files
older than a give date or number of days?

Thanks,
Jim

Torgeir Bakken

unread,
Aug 20, 2002, 12:24:40 PM8/20/02
to
Jim wrote:

> Anyone know of a script that will recursively delete files
> older than a give date or number of days?

Hi

Here is a Michael Harris script (slightly modified by me) that should do the
deed the correct way (by creating an array of the files first).


path = "c:\temp"
killdate = date() - 7

arFiles = Array()
set fso = createobject("scripting.filesystemobject")

' Don't do the delete/rename while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Creating an array of the file names first to avoid this.
'
SelectFiles path, killdate, arFiles

for n = 0 to ubound(arFiles)
set f = arFiles(n)
'=================================================
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
'=================================================
f.delete true
next

msgbox "Done"


sub SelectFiles(sPath,vKillDate,arFilesToKill)

'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
'
on error resume Next
dtcreated = file.datelastmodified
on error goto 0
if not isnull(dtcreated) Then
if dtcreated < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
end if
end if
next

' *****************************************
' disable section below to exclude subfoldes
'
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill
next

end sub

--
torgeir


Michael Harris (MVP)

unread,
Aug 20, 2002, 1:47:49 PM8/20/02
to
A slightly refined version - accounts for 'in use' files and adds a bIncludeSubFolders parameter to the SelectFiles procedure...

path = "c:\temp"
killdate = date() - 7

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)

'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(dtcreated) Then

if dtlastmodified < vKillDate 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


--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--


Torgeir Bakken

unread,
Aug 20, 2002, 2:20:06 PM8/20/02
to
"Michael Harris (MVP)" wrote:

> A slightly refined version - accounts for 'in use' files and adds a bIncludeSubFolders parameter to the SelectFiles procedure...

Perfect :)

This begins to look like a school book example I think :)

--
torgeir


Michael Harris (MVP)

unread,
Aug 20, 2002, 2:35:12 PM8/20/02
to

Only if one of us adds Option Explicit and the appropriate Dim statements ;-)...

grendizer

unread,
Aug 20, 2002, 3:45:08 PM8/20/02
to


Thanks for all those scripts and support! Why don't you make a book, the 2
of you? I would buy a copy for sure :)

--
bizous +++++ gz


John Braden

unread,
Aug 22, 2002, 10:28:45 AM8/22/02
to
How could we modify this to delete subfolders and possibly the original path
statement if the folder is empty?


"grendizer" <mystifi...@THISmystifier.net> wrote in message
news:uKtZ3HISCHA.1756@tkmsftngp11...

Torgeir Bakken

unread,
Sep 10, 2002, 7:58:14 PM9/10/02
to
"Michael Harris (MVP)" wrote:

> A slightly refined version - accounts for 'in use' files and adds a bIncludeSubFolders parameter to the SelectFiles procedure...

Bug found...

The line
"if not isnull(dtcreated) Then"

should be
"if not isnull(dtlastmodified) Then"

Below is an updated version...


sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)

if not isnull(dtlastmodified) Then


if dtlastmodified < vKillDate 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

--
torgeir


Torgeir Bakken

unread,
Sep 11, 2002, 5:56:14 AM9/11/02
to
Torgeir Bakken wrote:

> "Michael Harris (MVP)" wrote:
>
> > A slightly refined version - accounts for 'in use' files and adds a bIncludeSubFolders parameter to the SelectFiles procedure...
>
> Bug found...

Another bug found ;-)

arFiles(n).delete true
if err.number = 0 then
wscript.echo "Unable to delete: " & arFiles(n).path

is to be
arFiles(n).delete true
if err.number <> 0 then


wscript.echo "Unable to delete: " & arFiles(n).path

(= is substituted with <>)

I have also added "on error resume next" in the sub SelectFiles.

Below is an updated version...

A slightly refined version - accounts for 'in use' files and adds a bIncludeSubFolders parameter to the
SelectFiles procedure...

' folder to start search in...
path = "c:\temp"

' delete files older than 7 days...


killdate = date() - 7

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

0 new messages