Solved

Script help - deleting files older than certain number of days

Posted on 2006-11-03
7
1,138 Views
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.

Thanks
0
Comment
Question by:MCPJoe
7 Comments
 
LVL 10

Expert Comment

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


Aplimedia
0
 
LVL 3

Assisted Solution

by:schworak
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
    f.delete
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.

0
 
LVL 9

Author Comment

by:MCPJoe
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?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Accepted Solution

by:
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
next


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

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

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
            fso.DeleteFile(file.Path)
         End If
      End If
   Next

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

Expert Comment

by:schworak
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.
0
 
LVL 9

Author Comment

by:MCPJoe
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?  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now