Monorels
Monorels

Reputation: 1

Runtime Error Microsoft VBScript

Help with a script. Error: d: \ learning \ vbs \ backup6.vbs (47, 9) Runtime Error Microsoft VBScript: Invalid call or argument procedure. I can not understand why.

Dim Fso
Dim Directory
Dim Modified
Dim Files
Dim source
Dim destination
Dim rar
Dim n

source = "d:\test\source\"
destination = "d:\test\destination\"
rar = "d:\learning\vbs\Rar.exe"
n = 3

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run """rar"" a -agYYYY-MM-DD-HH-MM-SS " &destination& " " &source
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder(destination)
Set Files = Directory.Files
search_delete destination, n

Sub search_delete(str, n)
Dim strOldestFile
Dim dtmOldestDate
Dim count
Dim colFiles
Dim strFile

strOldestFile = ""
dtmOldestDate = Now
Set colFiles = Directory.Files

Do
    count = 0
    For Each objFiles in colFiles
        count = count + 1
        strFile = objFiles.Path
            dtmFileDate = objFiles.DateCreated
            If dtmFileDate < dtmOldestDate Then
                dtmOldestDate = dtmFileDate
                strOldestFile = strFile
            End If
    Next
    WScript.Echo(strOldestFile)

    If count > n Then
        Fso.DeleteFile strOldestFile
    End If  
    strOldestFile = ""

Loop While (count > n)

End Sub

error here in this line of code: Fso.DeleteFile (strOldestFile)

Upvotes: 0

Views: 861

Answers (1)

Dai
Dai

Reputation: 155145

Your program's logic isn't such that strOldestFile will contain a filename when count > n.

If you have a case where you have 3 files with ascending DateCreated time, then strOldestFile will never be set, yet Fso.DeleteFile will still be called.

Visual Studio (all versions, up-to and including 2015) includes a VBScript debugger. If you run a script using cscript //X //D $yourScriptFileName.vbs then it will prompt you to start VS and attach to the script host to step-through debug the script.

(Note that the //X //D command-line arguments really do have two forward-slashes)

Upvotes: 1

Related Questions