VBScript's FileSystemObject.DeleteFolder being cranky
December 21, 2009 8:03 AM   Subscribe

VBscript logon script uses FileSystemObject's DeleteFolder method to delete a local folder and randomly gets error #70 - Permission denied. I am at the end of my rope with this stupid, stupid thing.

We have a specific set of programs in a folder stored on a network share. I created a script that gets run in users' logon scripts and refreshes that folder from the network location. This is necessary because the users want that menu in their start menu, and that it should be in their preferred language (French/English). So the script does some checking for the users' language (MUI or not, pure french/english OS, etc) and then deletes their current local menu, then copies the network one over.

The problem is that once in every 10 logins or so (sometimes more, sometimes less, not reliably reproducible as far as I can see), the script will fail when trying to delete the local copy of the folder, giving error 70, permission denied.

Of course that's ridiculously silly, seeing as that local folder was copied under the user account itself and the user has full rights to the folder. Sillier than that, the script will actually manage to delete part of the local folder before it fails. Even sillier, it will never delete the same part; the leftover files and folders are pretty much different every time, so it's not one specific file with weird rights. And of course, if the user tries to delete the folder manually, there is absolutely no problem.

I have tried both the FileSystemObject's DeleteFolder method, and also tried to use GetFolder and then use that Folder object's Delete method. Both ways will eventually generate the error. I can sometimes get the error myself but like I said I can't reproduce it reliably and so I don't understand the conditions under which it occurs.

Are there any alternative ways that I could use to delete a folder using VBScript? Is this perhaps a known issue? Inquiring minds want to know.
posted by splice to Computers & Internet (3 answers total)
Something is accessing that folder and locking it. Some things to try:

1 - add a delay before your program runs; perhaps just after logging in, Windows is accessing the start menu in order to cache it, and thus there is contention.
2 - add a loop into your delete code, e.g., try five times in a row, swallowing the error 70 exception, before finally throwing it on the 6th failure. Add a small delay in the loop, too, e.g., 500ms, to give some time for any existing locks to release.
3 - Instead of deleting the folder, rename it with a unique but identifiable name (e.g., use a GUID for the name, and .deleteme for the extension.) Then your script can attempt to delete any folders with the extension .deleteme, but swallow the error if the delete fails.
posted by SNACKeR at 8:38 AM on December 21, 2009

SNACKeR is right. In Windows, you can't delete a file if it's in use. The shell is probably building an icon cache or something.
posted by sbutler at 8:47 AM on December 21, 2009

Hrm... Good suggestions. Will play with them tomorrow. Had thought of retrying and seeing if it unlocks after a short sleep but that fell by the wayside while I was working on it I guess. Thanks. Will update with results later.
posted by splice at 1:46 PM on December 21, 2009

« Older Help me fix my G string!   |   ...and they did it all with rocks and mud... Newer »
This thread is closed to new comments.