Script to delete all files more then a week old

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Script to delete all files more then a week old

Postposted on Mon Dec 20, 2010 2:51 pm

Total code newb here (apart from HTML/CSS but that hardly counts).

I need a script to delete all files older then a week from a certain directory and all it's subdirs, but not the folders themselves. I want it to work on XP and 7, since it needs to recognise the date that files were created I believe this means I'm looking at a VBScript.

Thought I'd post here and ask if anyone has such a script? Seems like a fairly common, simple task. If not I'll attempt to hack one together myself from what Google turns up and post what I come up with. Might be of use to some others.
Q6600 3.2GHz | P5K PRO | 4GB PC2-6400 | GTX 660 2GB | X25-M | IFX-14
puppetworx
Gerbil XP
 
Posts: 304
Joined: Tue Dec 02, 2008 4:16 am

Re: Script to delete all files more then a week old

Postposted on Mon Dec 20, 2010 3:01 pm

Here's some stolen vbscript modified, it will recurse now, works for me YMMV

Code: Select all
'************ Start of Code **********************

Option Explicit
'On Error Resume Next
Dim oFSO, oFolder, sDirectoryPath,strFolderPath, oSubFolder
Dim oFileCollection, oFile, sDir, oSubFolders
Dim iDaysOld, objShell, intDaysOld


set objShell = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")


' Specify Number of Days Old File to Delete

intDaysOld = 8


' Specify Directory Path From Where You want to clear the old files

sDirectoryPath = "F:\drived"


call DelOldFiles( intDaysOld , sDirectoryPath)



Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

function DelOldFiles(iDaysOld, StrFolderPath)


  Set oFolder = oFSO.GetFolder(strFolderPath)

  Set oFileCollection = oFolder.Files


  For each oFile in oFileCollection
      If oFile.DateLastModified < (Date() - iDaysOld) Then
      oFile.Delete(True)
      End If

  Next

  Set oSubFolders = ofolder.subfolders

  for each oSubFolder in oSubFolders

     call DelOldFiles (IdaysOld, oSubFolder.Path)

  next


end function

'**************** End of Code *******************
Last edited by axeman on Mon Dec 20, 2010 4:27 pm, edited 1 time in total.
badger badger badger badger badger badger badger
axeman
Minister of Gerbil Affairs
 
Posts: 2009
Joined: Fri Jan 31, 2003 10:46 am

Re: Script to delete all files more then a week old

Postposted on Mon Dec 20, 2010 3:43 pm

Working for me too, beautiful!

Was just starting to learn the basics when I saw you'd posted this. One of these days I will actually have to learn how this stuff works though. One of these days.

Sure more people will dig this too, thanks man!
Q6600 3.2GHz | P5K PRO | 4GB PC2-6400 | GTX 660 2GB | X25-M | IFX-14
puppetworx
Gerbil XP
 
Posts: 304
Joined: Tue Dec 02, 2008 4:16 am

Re: Script to delete all files more then a week old

Postposted on Mon Dec 20, 2010 4:30 pm

puppetworx wrote:Working for me too, beautiful!

Was just starting to learn the basics when I saw you'd posted this. One of these days I will actually have to learn how this stuff works though. One of these days.

Sure more people will dig this too, thanks man!


You're welcome. I'm sure some programmer type will tell me why the code sucks, but I'm not too interested in whether things suck or not, just whether they work... my day job is related to mass deployment of software, patches, etc. in a corporate environment and VBScript is the easiest thing that just works. Powershell is nice, but we're mostly XP for now, and throwing yet another requirement into the mix is not really advantageous. We have 2000 over custom "applications" ( use this term loosely as some of them are Access 2.0 databases, yes you read that right), and we get to try to deal with whatever mess the programmers duck tape together.
badger badger badger badger badger badger badger
axeman
Minister of Gerbil Affairs
 
Posts: 2009
Joined: Fri Jan 31, 2003 10:46 am


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest