Solved

Delete folders older than 30 days

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

772 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