Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1150
  • Last Modified:

Script help - deleting files older than certain number of days

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.

3 Solutions
Best move this this question to Visual basics... you are not going to get much help here in Javascript

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.

MCPJoeAuthor Commented:
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?
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
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.
MCPJoeAuthor Commented:
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?  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now