/build/static/layout/Breadcrumb_cap_w.png

delete folders by created date

Hey all,

I am looking for a script that would search a root folder for sub folders by created date then if the date is older than 2 weeks delete the folder and all of it's contents.

I found a post that is close to what i want but without the deleting and the 2 week old limitation.


I suck at VB so any help would be appreciated.

Thanks

0 Comments   [ + ] Show comments

Answers (6)

Posted by: anonymous_9363 14 years ago
Red Belt
0
As usual, for those who lead with the chin:- http://www.lmgtfy.com/?q=vbscript+delete+files+by+age
Posted by: jac 14 years ago
Yellow Belt
0
Okay so i found this script by someone else. This works perfectly except it is deleting the path folder as well. How do i get it to not do that?

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


' 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)

' uses error trapping around access to the
' Date property just to be safe
'

dtlastmodified = null
on error resume Next
dtlastmodified = folder.datecreated
on error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = folder
end if
end if



if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
next
end if
end sub
Posted by: dchristian 14 years ago
Red Belt
0
Hey Jac,

I reworked the script to not delete the root folder.

I also reworked the script to step through the array backwards so the count of the eligible folders is more accurate.

Hope this helps


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

root = Right(path,InStr(path,"\")+1)


' 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 = ubound(arFiles) To 0 Step -1
'=================================================
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
'=================================================
on error resume next 'in case of 'in use' files...
If(arFiles(n).Name <> root) Then
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
End if
next


msgbox nDeleted & " of " & ubound(arFiles) _
& " 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)

' uses error trapping around access to the
' Date property just to be safe
'

dtlastmodified = null
on error resume Next
dtlastmodified = folder.datecreated
on error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified > vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = folder
end if
end if



if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
next
end if
end sub
Posted by: anonymous_9363 14 years ago
Red Belt
0
I also reworked the script to step through the array backwards so the count of the eligible folders is more accurate. There is now a very long queue of mathemeticians outside your door. Go and talk to them: you're their hero. It isn't often that someone manages to completely alter the rules of arithmetic but you have apparently found how to change the way numbers work such that counting backwards instead of forwards makes the result different.
Posted by: pjgeutjens 14 years ago
Red Belt
0
42
Posted by: dchristian 14 years ago
Red Belt
0
you have apparently found how to change the way numbers work such that counting backwards instead of forwards makes the result different.

Deleting folders starting with the sub folders or "backwards" makes the count closer to the actual number of folders deleted.

The count of the deleted folders is incremented every time a folder is deleted.

The total number of eligible folder is found by taking a count of all the folders.

If you delete the child folders and work your way out the number of deleted folders is closer to the total number of folders.

If you start with the parent folder (deleting all children) you'll only get credit for the parent folder. Not the total number of sub folders contained within that parent as the original script was doing.

Looping through the array backwards was the quick way to make the counts more accurate.

Hope this makes sense. :)
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ