0

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
mirei
  • 11
  • 2

1 Answers1

2

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.

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • Thank you so much @AutomatedChaos... I didn't notice that there was an empty line in my input text file.. I didn't get the error anymore. – mirei Sep 09 '17 at 08:49
  • @mirei glad I could help. If this is the solution to your issue, you can tag this as the accepted answer and optionally upvote it. This gives virtual internet points to the answerer which seems to make people happy :) – AutomatedChaos Sep 09 '17 at 09:27