开发者

How do I pause VBS script while folder is deleted?

I'm writing a VBScript for SyncBack (backup utility) in which I am deleting old versions of a backup.

    '' # 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

However, occasionally the folder (that contains the old backup) which I am trying to delete takes longer that the 2 seconds at WScript.Sleep 2000 to remove and I was wondering if there was a way of making the script wait until the folder has been deleted before it prints out "Deleted <foldername> successfully".

Ideally I'd love something like:

While oFolder.IsDeleting() Then
    WScript.Echo "Still deleting..."
    WScript.Sleep 2000
Loop

But I'm well aware that that's probably not going to be the case.


While fso.FolderExists(DelFoldername)
   WScript.Echo "Still deleting"
   WScript.Sleep 1000
Wend


You could move the delete into it's own function and add a timeout. so if the folder is not deleted in a reasonable amount of time the function returns without deleting the folder. Something like this:

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 

Then call the function like this

If DeleteFolder("C:\New Folder", 5) Then
    Wscript.Echo "Folder Deleted"
Else
    Wscript.Echo "Folder could NOT be deleted" 
End If

WARNING: Using On Error Resume Next is not recomended.
This command will cause all errors to be ignored, and should only be used under adult supervision.

Side effects may include: Strange errors in other parts of a script, unexpected script action, headache, dizziness, confusion, anger, or a sudden urge to curse. In rare cases this command has been known to cause bleeding from the eyes and ears. Using this command may lead to hair and job loss.


Loop sleep with counter, note assumes CScript host which makes the "Still Deleting" message a bit more palettable. The delay in the example is 30 seconds.

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


Why not just keep checking to see if the folder still exists, and exit once it's gone?

' Do ' If folder does not exist, exit Do ' Loop

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜