Solved

Delete folders older than 30 days

Posted on 2006-06-21
5
895 Views
Last Modified: 2008-02-01
Ok, I have a script that runs every day to delete files that are older than 30 days from it.  This works great.  However, it does not remove any folders and I'd like it to remove those as well.  Here's my script:

Dim fso
Dim oFolder
Dim oFile

   Set fso = createobject("Scripting.FileSystemObject")
   Set oFolder = fso.GetFolder("C:\Temp")
   For Each oFile In oFolder.files
    If DateDiff("d", oFile.DateCreated,Now) > 30 Then
               oFile.Delete True
    End If
   Next

Set oFolder = Nothing
Set fso = Nothing
0
Comment
Question by:lkingpinl
  • 2
  • 2
5 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 50 total points
ID: 16952130
Have you tried the Delete Method?

Dim fso
Dim oFolder
Dim oFile

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set oFolder = fso.GetFolder("C:\Temp")
   For Each oFile In oFolder.Files
    If DateDiff("d", oFile.DateCreated, Now) > 30 Then
               oFile.Delete True
    End If
   Next
If oFolder.Files.Count Then
    oFolder.Delete
End If
Set oFolder = Nothing
Set fso = Nothing
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16952147
Sorry that should be

If oFolder.Files.Count = 0 Then 'missed the = 0 bit
    oFolder.Delete
End If
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 75 total points
ID: 16952247
this one will check for subfolders, and delete them when older than 30 days

Dim fso
Dim oFolder
Dim oFile
Dim oTmpFolder


   Set fso = CreateObject("Scripting.FileSystemObject")
   Set oFolder = fso.GetFolder("C:\Temp")
   For Each oFile In oFolder.Files
    If DateDiff("d", oFile.DateCreated, Now) > 30 Then
               oFile.Delete True
    End If
   Next
   
   For Each oTmpFolder In oFolder.SubFolders
      If DateDiff("d", oTmpFolder.DateCreated, Now) > 30 Then
         oTmpFolder.Delete True
      End If
   Next

Set oFolder = Nothing
Set fso = Nothing



please note that your code doesnt look if the file is modified!
it only checks to see when it is created, so it can still be in use.....
0
 
LVL 2

Author Comment

by:lkingpinl
ID: 16953620
I don't need it to look at Date Modified because the files stored here are supposed to be merely a data exchange folder.  Not a working folder.  
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16953714

ok
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB6 convert MSHFlexgrid1 cells 7 52
Window placement 17 65
Help in WHSCRIPT 9 41
VB 6.0 printer how to align 6 49
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

19 Experts available now in Live!

Get 1:1 Help Now