Link to home
Start Free TrialLog in
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

Avatar of No More
No More

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
Avatar of aikimark
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

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
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial