Solved

Delete Script - Permissions Denied Error

Posted on 2008-10-27
7
415 Views
Last Modified: 2012-05-05
I have borrowed this script from somewhere on here, and modified it a wee bit to apply to my environment:

This line:

If objSub.Size = 0 Then objFSO.DeleteFolder objFLD.Path  

Should delete the sub folders under the path, but of course I get Permissions Denied.  I tried to change the line to read:

If objSub.Size = 0 Then objFSO.DeleteFolder objFLD.Path, True

This should pass the variable to overwrite the read only attribute, but it still gives permissions denied.  Any thoughts?

I actually have the same problem with the files delete line:

objFSO.DeleteFile fil.Path


I though that I might be able to recursively change the read only attribute on all the folders and files before I  delete, but I have not been able to figure that out.

Thanks.
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
strSourceFolder = "F:\Common\Scans\"
iDaysOld = 0
dtOld = DateAdd("d", -iDaysOld, Date)
Set objFLD = objFSO.GetFolder(strSourceFolder)
 
For Each fld In objFLD.SubFolders
  Set objSub=objFSO.GetFolder(fld)
  For Each subFld in objSub.SubFolders
    ProcessFolder (objFSO.GetFolder(subFld))
  Next
  If objSub.Size = 0 Then objFSO.DeleteFolder objFLD.Path
  Set objSub=Nothing
Next
 
Set objFSO = Nothing
wscript.quit
 
Sub ProcessFolder(strSource)
  ProcessFiles strSource
  For Each fld In strSource.SubFolders
    ProcessFolder objFSO.GetFolder(fld)
  Next
End Sub
 
Sub ProcessFiles(strSrc)
  For Each fil In strSrc.Files
    If DateDiff("d", fil.datelastaccessed, dtOld) >= iDaysold Then
      objFSO.DeleteFile fil.Path
    End If
  Next
End Sub

Open in new window

0
Comment
Question by:Darksied9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Angelized
ID: 22811207
The DeleteFolder method does not distinguish between folders that have contents and those that do not. The specified folder is deleted regardless of whether or not it has contents.
I thing you should focus on why you are not able to delete the files,  File lock by another process,  permission denied, etc...
0
 
LVL 3

Author Comment

by:Darksied9
ID: 22811834
I know why...

objFSO.DeleteFile fil.Path
objFSO.DeleteFolder objFLD.Path

These two commands are supposed to take 1 argument (or input - whatever) "TRUE" and when that is specified it SHOULD overwrite the read only switch in the attributes.  I CAN manually remove the attributes and run the script and it works perfectly.  What this tells me is that...

objFSO.DeleteFile fil.Path, TRUE
objFSO.DeleteFolder objFLD.Path, TRUE

The two lines above don't pass the parameter for "True" properly.  What I am looking for is help in determining if I have done something completely wrong in assuming this will work, or for someone to help me figure out how to modify this script appropriately to change the attributes on everything inside the source folder recursively BEFORE attempting the deletes.

 
0
 
LVL 3

Expert Comment

by:Angelized
ID: 22811930
Hum, you lost me. I don't see why you want to do the recursive part yourself, as deletefolder will do for you. (wathever if the read only flag is set or not)
objFSO.DeleteFolder will delete the complete scructure anyway.
This scipt is equal the the above...
Unless you want to check each files and delete them on date/time basis ( i saw some unused code that let me assume this)
iDaysOld = 0
dtOld = DateAdd("d", -iDaysOld, Date)

 
 
 

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
strSourceFolder = "F:\Common\Scans\"
objFSO.DeleteFolder strSourceFolder,True
 
Set objFSO = Nothing
wscript.quit

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Angelized
ID: 22812093
Hum Sorry I have talked too fast 9and there is no way to edit)
The permissions denied is not coming fro the Read only attribut set it's comming form this line :
 If objSub.Size = 0 Then objFSO.DeleteFolder objFLD.Path
 The recursive process is modifiing the files you are trying the size for.
 
0
 
LVL 3

Author Comment

by:Darksied9
ID: 22812214
What I am actually doing is Parsing the F:\Common\Scans\ directory for all subfolders and files and deleting those.  I don't want to delete the Scans Directory, just everything in it.  I am looking to delete anything older than 1 day (the unused code is that way for testing purposes).

The script works perfectly except when a subfolder (e.g. F:\Common\Scans\Trippy\ ) has the read only attribute set.

I have tried to set the "True" variable in the code, but it is not taking it (the two lines below).


objFSO.DeleteFile fil.Path, TRUE
objFSO.DeleteFolder objFLD.Path, TRUE

Could it be as simple as removing the space between the "Comma" and "True" if it is I am going to close my head in a cupboard several times.
0
 
LVL 3

Accepted Solution

by:
Angelized earned 500 total points
ID: 22813276
Have you tried like this ?
objFSO.DeleteFolder strSourceFolder,True
0
 
LVL 3

Author Closing Comment

by:Darksied9
ID: 31510212
Thanks! I was trying to make a simple issue too difficult.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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