[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delete a folder - Access Denied!

Posted on 2009-02-12
5
Medium Priority
?
745 Views
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!
0
Comment
Question by:jmoran18251
5 Comments
 
LVL 14

Expert Comment

by:yehudaha
ID: 23623587
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
Next

Open in new window

0
 
LVL 5

Accepted Solution

by:
lodar123 earned 2000 total points
ID: 23623760
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 http://msdn.microsoft.com/scripting) 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
 
 
sUser="USERNAME"
sPass="PASSWORD"&VBCRLF
 
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
else
	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
	WScript.quit
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
 
wscript.Quit
 
 
'End of Script 

Open in new window

0
 
LVL 5

Expert Comment

by:lodar123
ID: 23623780
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
0
 
LVL 65

Expert Comment

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

Regards,

Rob
0
 

Author Closing Comment

by:jmoran18251
ID: 31546077
Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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