Solved

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

Posted on 2016-10-16
5
35 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
  • 2
5 Comments
 
LVL 6

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 51

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

17 Experts available now in Live!

Get 1:1 Help Now