Thanks,
Jim
> 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
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
-- 
> 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
Only if one of us adds Option Explicit and the appropriate Dim statements ;-)...
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
"grendizer" <mystifi...@THISmystifier.net> wrote in message
news:uKtZ3HISCHA.1756@tkmsftngp11...
> 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
> "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