运行时错误Microsoft VBScript

时间:2015-08-29 09:02:40

标签: vbscript

帮助编写脚本。 错误:d:\ learning \ vbs \ backup6.vbs(47,9)运行时错误Microsoft VBScript:无效的调用或参数过程。我不明白为什么。

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
这段代码中出现

错误:Fso.DeleteFile(strOldestFile)

1 个答案:

答案 0 :(得分:0)

您的计划的逻辑不是strOldestFilecount > n时包含文件名。

如果您的案例中有3个文件的升级时间DateCreated,那么strOldestFile将永远不会被设置,但仍会调用Fso.DeleteFile

Visual Studio(所有版本,包括2015版本)包括VBScript调试器。如果使用cscript //X //D $yourScriptFileName.vbs运行脚本,则会提示您启动VS并附加到脚本主机以逐步调试脚本。

(注意//X //D命令行参数确实有两个正斜杠)