0

I am new to VB Script, please help me with the below code.

I have a requirement where i need to delete all files in a folder older than x days and file name starting with xxxyyyzzz.log.

I have below code, but it is throwing

error as error in line 16

Const strPath = "C:\Users\xyz\Downloads"  
Dim objFSO  
Set objFSO = CreateObject("Scripting.FileSystemObject")

Call Search (strPath)  

WScript.Echo"Done."

Sub Search(str)  
    Dim objFolder, objSubFolder, objFile
    Set objFolder = objFSO.GetFolder(str)
    For Each objFile In objFolder.Files

        If Not ((objFile.filename = xxx*.log) And (objFile.DateCreated < (Now() - x)) Then
            objFile.Delete(True)
        End If

    Next
    For Each objSubFolder In objFolder.SubFolders
        Search(objSubFolder.Path)

            If (objSubFolder.Files.Count = 0) Then
            objSubFolder.Delete(True)
        End If

    Next
End Sub
Hackoo
  • 18,337
  • 3
  • 40
  • 70
Hara
  • 77
  • 1
  • 10
  • Did you mean delete all files with extension .log and older than x days ? – Hackoo Apr 06 '16 at 11:39
  • 1
    Possible duplicate of [VBScript - copy files modified in last 24 hours](http://stackoverflow.com/questions/3004797/vbscript-copy-files-modified-in-last-24-hours) – MikeC Apr 06 '16 at 11:44
  • Where did you get your example from `File` object doesn't have a `Filename` property? As @MikeC point's out there is already a ton of varying examples on [so] on how to do this, we don't need yet another. – user692942 Apr 06 '16 at 11:45
  • There are several questions on SO (with answers) that will help you go through all files in a folder, filter them by name and date and then do some file operation on them. An example is [http://stackoverflow.com/questions/3004797/vbscript-copy-files-modified-in-last-24-hours](http://stackoverflow.com/questions/3004797/vbscript-copy-files-modified-in-last-24-hours?rq=1) can be a beginning. – MikeC Apr 06 '16 at 11:46
  • Hi Hackoo, Yes i want to delete all files with name starting xxx*.log and the last modified date is 10 days before. Hi MikeC and Lankymart, I have tried all the links you gave and was unable to make out what is wrong in my code. Can you guys please help me to correct the above code. – Hara Apr 09 '16 at 05:40
  • Please help me to correct the below statement If Not ((objFile.filename = xxx*.log) And (objFile.DateCreated < (Now() - x)) Then objFile.Delete(True) End If – Hara Apr 11 '16 at 03:36

0 Answers0