Solved DeleteFolder vbs Called From

April 2, 2012 at 16:50:16
Specs: Windows 7
I have a vbs that runs an uninstall routine which works as expected with one minor exception. I cannot delete the folder that the vbs is called from. I have the coding in the vbs to run with elevated rights

If WScript.Arguments.Named.Exists("elevated") = False Then
	'Launch the script again as administrator
	CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1

I am closing the folder right away at the beginning of the script:

Set oShell = CreateObject("WScript.Shell") 
oShell.AppActivate "msys"
oShell.SendKeys "%{F4}"

And for good measure deleting the vbs itself towards the end and included a sleep before the folder is deleted.

objFSO.DeleteFile WScript.ScriptFullName
WScript.Sleep 30
objFSO.DeleteFolder "C:\folder1\", True

The error I am getting is access denied. I have correct rights as this runs fine if I were to launch if from C:\folder2 just not if it is lanched from C:\folder1. I'm at a loss as to why this would be since its the folder isnt open, isnt locked and is empty by the time I call the DeleteFolder. I additionally tried to add in the RD comment with the exact same results.

WshShell.Run "%COMSPEC% /C RD C:\folder1 /S /Q", 0

See More: DeleteFolder vbs Called From

Report •

April 3, 2012 at 05:16:23
✔ Best Answer
Assuming the script started in folder1, it's probably blocking your delete. Try using WScript.Shell.CurrentDirectory to change your current directory to something other than c:\folder1.

How To Ask Questions The Smart Way

Report •

April 3, 2012 at 09:57:22
Thanks yea, it was a long day yesterday and I realized my mistake after I had posted this. Just hadnt gotten a chance to followup yet but that was it. Sometimes ya need some distance or another set of eyes! Thanks for the response.

Report •
Related Solutions

Ask Question