Avatar of Emma Karlsson
Emma Karlsson

asked on 

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

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

VB ScriptMicrosoft DOS

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon