Vbscript 删除文件夹时如何暂停VBS脚本?

Vbscript 删除文件夹时如何暂停VBS脚本?,vbscript,Vbscript,我正在为SyncBack(备份实用程序)编写一个VBScript,其中我正在删除备份的旧版本 '' # if current version is greater than the total allowed number of versions, '' # delete the oldest folder If versionNumber > totalVersions Then delDirNum = versionNumber - totalV

我正在为SyncBack(备份实用程序)编写一个VBScript,其中我正在删除备份的旧版本

    '' # if current version is greater than the total allowed number of versions,
    '' # delete the oldest folder
    If versionNumber > totalVersions Then
        delDirNum = versionNumber - totalVersions
        If delDirNum > 0 Then
            DelFoldername = containerFileOrFolder & "\" & delDirNum & "_"
               '' " # Ignore this line SO Prettify doesn't do VB very well. 
            If fso.FolderExists(DelFoldername) = True Then
                WScript.Echo "Deleting: <" & DelFoldername & ">"
                Set oFolder = objFileSystem.GetFolder(DelFoldername)
                oFolder.Delete()
                WScript.Sleep 2000
                If fso.FolderExists(DelFoldername) = False Then
                    WScript.Echo "Deleted <" & DelFoldername & "> successfully"
                Else
                    WScript.Echo "Could not delete <" & DelFoldername & ">"
                End If
            End If
        End If
    End If

但我很清楚,情况可能不是这样。

您可以将delete移到它自己的函数中,并添加一个超时。因此,如果文件夹没有在合理的时间内删除,函数将返回,而不删除文件夹。大概是这样的:

While fso.FolderExists(DelFoldername)
   WScript.Echo "Still deleting"
   WScript.Sleep 1000
Wend
Function DeleteFolder(Folder, Timeout)
'Folder is the full file path of the folder
'Timeout is the amount of time to wait for the folder to be deleted (in seconds).
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists(Folder) Then
        fso.DeleteFolder Folder, True
        start = Now()
        do while fso.FolderExists(Folder) AND DateDiff("s",start,Now()) < Timeout
            wscript.sleep 1000
        Loop
    End If
    If fso.FolderExists(Folder) Then
        DeleteFolder = False
    Else 
        DeleteFolder = True
    End If
End Function 
警告:不建议使用错误恢复下一步。
此命令将导致忽略所有错误,并且只能在成人监督下使用


副作用可能包括:剧本其他部分的奇怪错误、意外的剧本动作、头痛、头晕、困惑、愤怒或突然想要诅咒。在极少数情况下,这种命令会导致眼睛和耳朵出血。使用此命令可能会导致头发和工作丢失。

使用计数器循环sleep,注意:假定使用CScript主机,这会使“仍在删除”消息更加苍白。示例中的延迟为30秒

Dim loopCount : loopCount = 0
WScript.StdOut.Write "Deleting ."
Do While fso.FolderExists(DelFoldername) And loopCount < 30
   WScript.Sleep 1000
   WScript.StdOut.Write "."
   loopCount = loopCount + 1
Loop
WScript.StdOut.Write vbCrLf
If fso.FolderExists(DelFolderName) Then
   '' # Do stuff due to failure.
End If
Dim loopCount:loopCount=0
WScript.StdOut.Write“删除”
当fso.FolderExists(DelFoldername)和loopCount<30时执行
WScript.Sleep 1000
WScript.StdOut.Write“
loopCount=loopCount+1
环
WScript.StdOut.Write vbCrLf
如果fso.FolderExists(DelFolderName),则
“因为失败而做事。
如果结束

为什么不继续检查文件夹是否仍然存在,并在文件夹消失后退出

”“是吗 '如果文件夹不存在,请退出Do
'Loop

objFileSystem的类型是什么?FileSystemObject:这就是我要做的,但是如果脚本不能删除文件夹呢?无限循环?如果有错误恢复,则错误将自动中止。下一步为什么要使用错误恢复?因为我不关心此函数中的错误,我只关心文件夹是否被删除。如果脚本遇到错误,它应该中止吗?从OP的代码来看,我想说他们不希望脚本停止,他们只是想知道文件夹是否被删除。除了猜测删除文件夹所需的最长时间并以此作为限制外,真的没有其他方法吗?@Tester101:你确定当发生任何可能的错误时,函数在所有情况下都返回适当的真值或假值吗?@Jon:除了删除一堆大小相同、包含相同文件类型、大小和时间相同的文件夹外,我想不出任何方法来知道删除一个文件夹需要多长时间。然后你可以取一个平均值作为超时,除此之外,我想不出一种方法来知道该操作需要多长时间。除了猜测删除文件夹所需的最长时间并以此作为限制外,真的没有其他方法吗?您担心无限期等待,因此必须有一个限制。因此,问题变成了“如何确定在确定文件夹删除确实失败之前等待的最长时间?”。这是一个无法回答的问题,但我们可以运用一些常识,将其修改为“最长合理时间是多少……”。我的建议是,用300秒,然后等待,看看有多少次,结果证明没有涵盖它,我的钱是在没有。我担心的是,等待无限期的情况下,文件夹无法删除@Tester101中包含的“On Error…”非常有用,因为这样可以确保在删除文件夹及其内容时发生错误时,等待不是无限的。除此之外,我可能必须按照您的建议弥补一个限制。我想管理脚本的方法与典型的开发人员实践有所不同。我会避免下一步的错误恢复,就像瘟疫一样,把它藏起来,在一个小函数中用它保护一行代码,当然不会让它凌驾于任何杂项代码之上。在这种情况下,不必担心错误会导致无限循环,因为错误会做它应该做的事情。希望它能让你在下一步使用我的错误恢复时感到更轻松。
If DeleteFolder("C:\New Folder", 5) Then
    Wscript.Echo "Folder Deleted"
Else
    Wscript.Echo "Folder could NOT be deleted" 
End If
Dim loopCount : loopCount = 0
WScript.StdOut.Write "Deleting ."
Do While fso.FolderExists(DelFoldername) And loopCount < 30
   WScript.Sleep 1000
   WScript.StdOut.Write "."
   loopCount = loopCount + 1
Loop
WScript.StdOut.Write vbCrLf
If fso.FolderExists(DelFolderName) Then
   '' # Do stuff due to failure.
End If