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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tonyjoewalkerAuthor Commented:
DOH!  I knew that.  :)  I'll test that out.... thanks!
tonyjoewalkerAuthor Commented:
Yup, that did the trick.  Thank you.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was it.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.