VBScript Delete Folder Error


I have a working VBScript that deletes old symantec AV defs from servers that have less than 3 GB free on the C drive.  On a couple of servers, the old virus defs are locked by the service and they can't be deleted.  The obvious solution is to stop the service, then delete the folder in question.  Everything else in the script works perfectly.

What I have done is added If err.number <> 0 then call a sub that will stop the service, delete folder, start service (right now it just echos an ERROR line).

What I am running into is that when the script attempts to delete the file, gets an error, and then script then jumps to the next server in the list, so it never gets to the line with if err.number <> 0.  It also doesn't finish checking the rest of the files in that folder.  On Error Resume Next is set at the beginning of the script.

Here is the part of the code in question:

If oSubFolder.Name <> oNewestFolder Then
      Wscript.Echo "Deleting : " & oSubFolder.Name
      ResFile.writeline vbtab & vbtab & "DELETING " & oSubFolder.Name & vbtab & oSubfolder.DateCreated & vbtab & oSubfolder.size
      oSubfolder.Delete, True
      IF Err.number <> 0 Then
            Call StopService(strcomputer)
      END IF
End If

SUB StopService(strComputer)

wscript.echo vbtab & vbtab & "*ERROR*  Need to stop Symantec Service"
ResFile.writeline vbtab & vbtab & "*ERROR*  Need to stop Symantec Service"

Who is Participating?
Bill PrewCommented:
Is your code in a function or subroutine, if so then you need to also include the On Error Resume Next at the top of the routine as well.  Putting it once at the top of the script only activates it for the main body, not for subroutines and functions (something I learned the hard way).

tonyjoewalkerAuthor Commented:
DOH!  I knew that.  :)  I'll test that out.... thanks!
tonyjoewalkerAuthor Commented:
Yup, that did the trick.  Thank you.
Bill PrewCommented:
Welcome, glad that was it.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.