Gunjan
Gunjan

Reputation: 1

While executing delete script getting error wrong number of arguments or error invalid property assignment:' objFolder.Files.Item'

I have written VBScript to delete the files from the folder which are 30days older. even I need to add entry in log file. But here I need to list the name of the files which got deleted in log file. while doing that I am getting error.

  Const strPath = "D:\TextReport\"
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set LogFile = objFSO.CreateTextFile("D:\Text\ASD.txt", true)
    LogFile.WriteLine "DateTime: " & Now
    LogFile.WriteLine "UserName: " & CreateObject("WScript.NetWork").UserName

    Call Search (strPath)

    ' Comment out below line if you'd like to use this script in windows schedule task
    WScript.Echo"Done."

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

            ' Use DateLastModified for modified date of a file
            If objFile.DateLastModified < (Now() - 30) Then
                objFile.Delete(True)
            End If  
        LogFile.WriteLine (objFolder.Files.Item.Name)
        Next
        For Each objSubFolder In objFolder.SubFolders
            Search(objSubFolder.Path)

            ' Files have been deleted, now see if the folder is empty.
            If (objSubFolder.Files.Count = 0) Then
                objSubFolder.Delete True
            End If

        Next

    End Sub

    LogFile.Close
    WScript.Echo "Finished"

Upvotes: 0

Views: 139

Answers (1)

Rno
Rno

Reputation: 887

You are trying to get the name of a file that you deleted. Log the name inside the date check.

If objFile.DateLastModified < (Now() - 30) Then
    LogFile.WriteLine objFile.Name
    objFile.Delete(True)
End If 

Upvotes: 0

Related Questions