[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-10-16
5
Medium Priority
?
77 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 46

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 46

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 57

Accepted Solution

by:
Bill Prew earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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