Solved

Delete folders older than 30 days

Posted on 2006-06-21
5
900 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

860 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