0
votes

I wrote a script to grab the list for folders from a file and it will check and delete for files that are more than 90 days old.

The script was able to delete the files older than 90 days. However I keep on getting an error saying:

D:\cleanup90days.vbs(25, 3) Microsoft VBScript runtime error: Invalid procedure call or argument

I don't have an idea what I have missed. Any help will be appreciated.

Below is my script:

Dim days
Dim inputFolderList, ObjFolder, Files, objFileAge

If Not WScript.Arguments.Count = 2 Then
    Wscript.Echo "Invalid number of arguments. Arg1: Daily or Weekly. Arg2: Remove all files older then this"
    WScript.Quit(-1)
End If

days = WScript.Arguments.Item(1)

inputFileList = "D:\FileGrep2.txt"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(inputFileList, 1)

Do Until objTextFile.AtEndOfStream
    sFolderName = objTextFile.ReadLine
    getfoldernames(sFolderName)
Loop

Function getfoldernames(sFolderName)
    Set ObjFolder = fso.GetFolder(sFolderName)
    Set Files = ObjFolder.Files

    For Each Check In Files
        objFileAge = DateDiff("n", Check.DateLastModified, Now)
        If objFileAge > 90 Then
            WScript.Echo Now & "the following will be deleted " & Check.Path
            Check.Delete
        End If
    Next
End Function
1
Your script looks ok ... is there anything else going on in your script or is this the complete script? - Pankaj Jaju
check if sFolderName is blank before calling getfoldernames(sFolderName) - Flakes
Line 25 in the code you posted is objFileAge = DateDiff("n", Check.DateLastModified, Now). Is that really the line raising the error? - Ansgar Wiechers
I don't get any errors on your script. Please note that "n" in your DateDiff stands for minutes and not days (use "d" instead). - AutomatedChaos

1 Answers

2
votes

Probably, you've got an empty line in your input file "D:\FileGrep2.txt" causing the Set ObjFolder = fso.GetFolder(sFolderName) line throwing this error.