Solved

Delete folders older than 30 days

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now