Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script help - deleting files older than certain number of days

Posted on 2006-11-03
7
Medium Priority
?
1,147 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
[X]
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
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 328 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
Independent Software Vendors: 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

by:
rettiseert earned 344 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 328 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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'…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

704 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