Solved

Need help with my vbscript, trying to delete files and folders older than 1 day

Posted on 2016-10-16
5
52 Views
Last Modified: 2016-11-08
hi!
I have a vbscript that I want to run to delete all files and folders that are older than 24 hours, and it should NOT delete a directory if it has files or folders in it that is younger than 24 hours. But when I try to run my script i get an error saying
Line: 19,
Char: 4
Error: Ojbect doesn't support this property or method
Code: 800A01B6

Which means there is something wrong with this line in the script
"If Flag = "yes" then objSubFolder.Delete(True)"
But I don't know what's wrong with it.

I appreciate all the help i can get.

This is my current script:
 Const strPath = "D:\shared\temp"
 Dim objFSO
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Call Search (strPath)
Sub Search(str)
 Dim objFolder, objSubFolder, objFile
 Set objFolder = objFSO.GetFolder(str)
 For Each objFile In objFolder.Files
  If objFile.DateCreated < (Now() - 1) Then
   objFile.Delete(True)
  End If
 Next
 For Each objSubFolder In objFolder.Subfolders 
  Flag = ""
  If objSubFolder.DateCreated < (Now() - 1) Then
   For Each Thing in objSubFolder
    If thing.DateCreated > Now() - 1 then Flag="yes"
   Next
   If Flag = "yes" then objSubFolder.Delete(True)
  End If
 Next
End Sub

Open in new window

0
Comment
Question by:Emma Karlsson
[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
5 Comments
 
LVL 7

Expert Comment

by:No More
ID: 41846026
Check this link : http://itknowledgeexchange.techtarget.com/system-administration/vbscript-to-delete-files-in-a-directory-older-than-x-days/

Don't forget to change :
strDir = "FULL\PATH\TO\FOLDER\TO\BE\CLEANED"
strDays = 7
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41848456
1. Do you have an Option Explicit statement?
2. I suspect that line 19 should be:
If Flag = "yes" then objSubFolder.Delete True

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 41848462
3. I am not a fan of deleting items with the iteration variable.  I would use the name of the folder (= iteration variable property) in a deletion operation of the FSO object
1
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points (awarded by participants)
ID: 41851312
See if this makes sense and give it a test.

' Require variables to be defined
Option Explicit

' Define base folder to purge files from
Const strPath = "D:\shared\temp"

' Global variables
Dim objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")

Call Search (strPath)


Sub Search(strFolder)
    ' Local variables
    Dim objFolder
    Dim objFile
    Dim objSubFolder
    Dim objCheckFile
    Dim objCheckFolder
    Dim Flag

    ' Acess the folder
    Set objFolder = objFSO.GetFolder(strFolder)

    ' Check each file in this folder
    For Each objFile In objFolder.Files

        ' If file is old enough delete it
        If DateDiff("h", objFile.DateCreated, Now()) >= 24 Then
            objFile.Delete True
        End If

    Next

    ' Now check all folders
    For Each objSubFolder In objFolder.Subfolders

        If DateDiff("h", objSubFolder.DateCreated, Now()) >= 24 Then
            ' Assume we can delete it
            Flag = "yes"

            ' Any new files that prevent deletion
            For Each objCheckFile in objSubFolder.Files
                If DateDiff("h", objCheckFile.DateCreated, Now()) < 24 Then
                    Flag = "no"
                End If
            Next

            ' Any new folders that prevent deletion
            For Each objCheckFolder in objSubFolder.Folders
                If DateDiff("h", objCheckFolder.DateCreated, Now()) < 24 Then
                    Flag = "no"
                End If
            Next

            ' Delete folder if no newer items found
            If Flag = "yes" Then
                objSubFolder.Delete True
            End If
        End If

    Next

End Sub

Open in new window

~bp
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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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