How to suspend a VBS script while deleting a folder?

I am writing VBScript for SyncBack (backup utility) in which I delete old versions of the 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, sometimes the folder (containing the old backup) that I am trying to delete takes longer than 2 seconds in WScript.Sleep 2000 , and I was wondering if there is a way to make the script until the folder is deleted before how it displays "Deleted <folder name> successfully".

Ideally, I would like something like:

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

But I know well that this probably will not happen.

+3
source share
4 answers

, CScript, "Still Deleting" . 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
+1
While fso.FolderExists(DelFoldername)
   WScript.Echo "Still deleting"
   WScript.Sleep 1000
Wend
+3

-. , , . - :

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 

,

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

: On Error Resume Next .
.

: script, script, , , , . , , . .

+2
source

Why not just check if the folder exists and exit after deleting it?

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

0
source

Source: https://habr.com/ru/post/1723456/


All Articles