Script help - deleting files older than certain number of days

Posted on 2006-11-03
Last Modified: 2008-01-09
I have various sets of folders in a particular folder structure on a windows machine, that I have my backups going to.  My backup runs a backup to disk job that dumps the B2D files in various folders for various servers.  What I need to do is retain 3 days worth of these B2D files, and get rid of the rest.  Every night I run a backup of the B2D Files to tape, so deleting them is ok.  Can someone help me put together a VB script (or whatever method is best) to delete .bkf files with a data modified of older than 3 days.  I just want to keep 3 days worth of files, so anything older than 3 days can be deleted.  Any help would be greatly appreciated.

Question by:MCPJoe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 17867484
Best move this this question to Visual basics... you are not going to get much help here in Javascript


Assisted Solution

schworak earned 82 total points
ID: 17867679
Dim fso As Object
Dim f As Object
Set fso = CreateObject("scripting.filesystemobject")
For Each f In fso.GetFolder("C:\").Files
    Debug.Print f.Name, f.DateLastModified
Next f
Set f = Nothing
Set fso = Nothing

Now this sample will only list the files and date they were last modified. To actually make the script delete, you will need to change the DEBUG.PRINT to this...

if f.datelastmodified < dateadd("d",-7,now()) then
end if

The "d" means we are adding days
The -7 means we are adding minus seven days or subtracting 7 days from today's exact date and time.

Play with it and get it to do what you really want.

PS: Don't forget to run this on a test forlder or bad things will happen to your files.


Author Comment

ID: 17867785
So if I only wanted it to dele files older than 3 days, I would change the 7 in your code to a 3, and add the debug.print in the end of your message to replace the existing debug.print line.  And that should do it right?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Accepted Solution

rettiseert earned 86 total points
ID: 17867804
Try this (change path to your directory and days if you want another age limit). Be sure to try on a test/backup directory first

Option Explicit

Const path = "c:\Copia de MelateX"
Const days = 3

Dim Text
Dim fso, oFolders, oFolder, oSubFolders, wsh ' Object variables
Dim oFile, oFiles

Set wsh = WScript.CreateObject("WScript.Shell")

' Create FileSystemObject object to access the file system.
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' Get Folders collection.
Set oFolders = fso.GetFolder(path)  
Set oSubFolders = oFolders.SubFolders

for each ofile In oFolders.Files
      DelFile ofile, days

For Each oFolder In oSubFolders     ' All folders
    for each ofile In oFolder.files
            DelFile ofile, days

Sub DelFile(ofile, MaxAge)
      if fix(now) - fix(ofile.DateLastModified ) > MaxAge then
      end if
End sub
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 82 total points
ID: 17868437
Another one:

Dim fso, startFolder, OlderThanDate

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "C:\some\folder\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -3, Date)  ' 3 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If UCase(Right(file.Name, 4)) = ".BKF" Then
         If file.DateLastModified < BeforeDate Then
         End If
      End If

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
End Function

Expert Comment

ID: 17869771
All variations on the same theme. Each would work depending on the particulars of your delete needs.

I use this concept in a backup process of my own with a little more complexity. I backup each day to a different folder based on the date (backup_061103 for example) and I keep 7 backup folders regardless of how old they are and delete the oldest ones first.

I do this just in case a backup fails to run one day I still have 7 backup folders. (which actually happens once in a while) I learned this after going by date and my backup kept failing and one day I realized I had NO BACKUPS at all because all folders were over 7 days old. Good thing nothing got lost while I was on vacation.

I used the DIM ... AS OBJECT so I wouldn't have to reference the scripting object but if you do reference it you get some nice features while in the editor. Functionally, it makes no difference.

Author Comment

ID: 17869848
Thanks for the input.  I am going to try a few of these and see which one works best for my needs.  Its really simple, I just need something to start at a root level folder, delete any .bkf file with a modified date older than 3 days.  One tricky part I just thought of would be the weekend.  We are about to start a 7 day a week rotation, but currently are only doing 5.  So how would I handle the weekend, because the on Sunday night the modified date would delete some valid backups I want to keep.  Is there a way in this script that would allow for the weekend too and not delete Wednesday's backup until Monday?  

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question