?
Solved

Delete folders older than 30 days

Posted on 2006-06-21
5
Medium Priority
?
906 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
[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
  • 2
  • 2
5 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 150 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 225 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

Industry Leaders: 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!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

770 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