Delete a folder - Access Denied!

Posted on 2009-02-12
Last Modified: 2013-12-20
I am trying to write a small program users can run to delete a folder and its contents.  While I have found several different ways to delete a folder, what I need is some way to get around the permissions set on the folder I am trying to delete.  It works fine when I run it, but when I log in as another user I get 'Access Denied' because of course... they do not have permissions to the folder.

Without too many boring details, when users open a certain program for the first time, it pulls down its database from the server and stores it on the local machine and works from that.  For some reason over time the database is becoming corrupt.  If the folder and contents is deleted, the next time they log on, it will recreate the folder and the data... eliminating the problem.  I do not want to make everyone admins, so I need a way around the NTFS permissions on that folder and contents so they can blow it away.  Any ideas?  Thanks!
Question by:jmoran18251
    LVL 14

    Expert Comment

    insert user name and password between ""

    UserName = ""
    Password = ""

    folder name here:
    Win32_Directorywhere Name = 'c:\\Scripts'",,48)

    try this:
    strComputer = "."
    UserName = ""
    Password = ""
    Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Directorywhere Name = 'c:\\Scripts'",,48)
    For Each objItem in colItems
    	errResults = objItem.Delete
        Wscript.Echo errResults

    Open in new window

    LVL 5

    Accepted Solution

    1. Why not set permissions on that folder for everyone?
    2. You can use the following script to execute a command via RUNAS. You might find this helpful for yourself, or run it from a batch file (although the password will be in clear text). Another alternative is to hardcode the command, username and password in the script, then use the Script Encoder (download from to encode it. You run the script the same way, the file will just have a different extension: cscript vbrunas.vbe. Then if you have repeatable admin tasks you or your users, just execute the script.

    On Error Resume Next
    dim WshShell,oArgs,FSO
    set oArgs=wscript.Arguments
    sCmd="cscript SCRIPT.VBS"
    set WshShell = CreateObject("WScript.Shell")
    set WshEnv = WshShell.Environment("Process")
    WinPath = WshEnv("SystemRoot")&"\System32\runas.exe"
    set FSO = CreateObject("Scripting.FileSystemObject")
    if FSO.FileExists(winpath) then
    	'do nothing
    	WScript.echo "!! ERROR !!" & VBCRLF & "Can't find or verify " & winpath &"." & VBCRLF & "You must be running Windows 2000 or later for this script to work."
    	Set WshShell=Nothing
    	Set WshEnv=Nothing
    	Set oArgs=Nothing
    	Set FSO=Nothing
    End if
    rc=WshShell.Run("runas /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE)
    Wscript.Sleep 1000 'need to give time for window to open.
    WshShell.AppActivate(WinPath) 'make sure we grab the right window to send password to
    WshShell.SendKeys sPass 'send the password to the waiting window.
    set WshShell=Nothing
    set oArgs=Nothing
    set WshEnv=Nothing
    set FSO=Nothing
    'End of Script 

    Open in new window

    LVL 5

    Expert Comment

    If you encode the script i gave you, password wont be in clear text.
    Also that script permits you to run any vbs you want.  so you just execute it and your done.
    Hope it helps
    LVL 65

    Expert Comment

    Just give "Authenticated Users" full rights to the folder, then, upon logon, they will be able to delete the folder.



    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now