?
Solved

Windows Scripting VBS: fso.deleteFolder error 800a0046 - permission denied

Posted on 2003-03-21
15
Medium Priority
?
3,506 Views
Last Modified: 2010-05-18
I have written a very simple script that looks for a certain folder name on our server, verifies it is the target and then deletes it.  It all works fine except for the actual delete statement.  The script is:

' This script runs through every directory below the path specified as Root and deletes all folders with the path ..\AppData\Internet\History.
' It should be scheduled to run once a week on a Connect 2.4 server to keep the internet temporary files under control.
' Written by Jamie Kirkpatrick - Network Administrator, ex7103

'Set the initial variables
dim rootPath, fso, ws, logFile,
'Insert the root path here - this should be the Users folder on the server.
rootPath = "D:\Users"
'Create the FileSystem Object
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Shell object for user interaction
set ws = WScript.CreateObject("WScript.Shell")

call initiateLog
'Begin stepping through the tree
stepThroughTree ( rootPath )
call cleanUp

sub cleanUp
      logFile.writeLine ("Cleanup finished at : " & now ())
      logFile.close
      set fso = nothing
      set ws = nothing
end sub

Function stepThroughTree ( folder )
      Dim folderList, item, folderObj, deleteTarget
      Set folderObj = fso.GetFolder( folder )
      Set folderList = folderObj.SubFolders
      For Each item In folderList
            If isTarget (item) Then
                  'Delete the folder
                  logFile.write ("Deleting the folder: " & item.ParentFolder.Path & "...")
                  deleteTarget = item.ParentFolder.path
                  fso.deleteFolder ( deleteTarget, true )
                   'Log the deletion
                   logFile.writeLine ("OK")
            Else
                  stepThroughTree ( item )
            End If
      Next
End Function

' Function to check that the folder is a \AppData\Internet\History
Function isTarget ( item )
      if ((item.name = "History") and (item.ParentFolder.Name = "Internet") and (item.ParentFolder.ParentFolder.Name = "AppData")) then
            isTarget = True
      end if
End Function

' Subroutine to check if the correct directory structure eists for logging, and to create a new logfile
Sub initiateLog
      ' Intitiate variables
      dim systemLogs, logFileName, logPath
      Const ForWriting = 2
      'Check whether the log folder exists > if not create it
      if not fso.folderexists ("C:\WINNT\system32\LogFiles\cleanupScripts") then
            set systemLogs = fso.createfolder ("C:\WINNT\system32\LogFiles\cleanupScripts")
      end if
      'Create a new log file for the day
      logFileName = date()
      logFileName = replace (logFileName,"/","-")
      logPath = "C:\WINNT\system32\LogFiles\cleanupScripts\" & logFileName & ".log"
      Set logFile = fso.OpenTextFile(logPath, ForWriting, True)
      'Write some intiation lines to the log
      logFile.writeLine ("Cleaning the Internet folders from user areas.")
      logFile.writeLine ("Process began: "& now())
end sub

I have tried various ideas, and have scoured the internet for answers to this problem, but the trouble with most of them is that they invariably relate to asp.  this is not asp, it is only vbs.  

so here is what i have tried:
1: chaging the path of the target so it has a trailing slash                   deleteTarget = item.ParentFolder.path & "\"
fso.deleteFolder ( deleteTarget, true )
I have tried two slashes as that has also been suggested in some workarounds.
2: using different delete methods.  IE referecing an object's delete method by opening the folder as an object reference and using folder.delete
3: as you can see i have used the force option of the function.
4: I wrote a stripped down script with just the trouble sections in and it still fails:
'Create the FileSystem Object
Set fso = CreateObject("Scripting.FileSystemObject")
deleteTarget = "C:\targetfolder\deleteme"
fso.deleteFolder ( deleteTarget, true
5: Enabling auditing on the folder i am trying to delete - this doesnt seem to show any failures.
6: Using filemon to monitor the access atempt. This crashed the server twice.  

Long and the short is - im lost and it seems ridiculous that esuch a basic scripting function should cause such trouble.  oh yeah - i suppose the server details would help.
its a straight nt 4 server sp6a

thanks in advance.

jamie
0
Comment
Question by:jamiekoxford
[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
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8181138
It appears that you are trying to delete when it is in use.
0
 

Author Comment

by:jamiekoxford
ID: 8181168
i dont see how that is the case, even with _only_ this code it doesnt work:

'Create the FileSystem Object
Set fso = CreateObject("Scripting.FileSystemObject")
deleteTarget = "C:\targetfolder\deleteme"

there is no opening of the folder there, only a command that says delete me.  

also, i dont think it is in use in my script above - could you point out where?
0
 

Author Comment

by:jamiekoxford
ID: 8181443
i dont see how that is the case, even with _only_ this code it doesnt work:

'Create the FileSystem Object
Set fso = CreateObject("Scripting.FileSystemObject")
deleteTarget = "C:\targetfolder\deleteme"

there is no opening of the folder there, only a command that says delete me.  

also, i dont think it is in use in my script above - could you point out where?
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 16

Expert Comment

by:Richie_Simonetti
ID: 8183924
i got the same error as you but the folder "History" is deleted anyway.
0
 

Author Comment

by:jamiekoxford
ID: 8193795
It doesnt delete on my system unfortunately.  This is a well known error.  There must be a solution!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8196488
see if this help:

Function stepThroughTree(folder)
     Dim folderList, item, folderObj, deleteTarget
     Dim bolFound
     Set folderObj = fso.GetFolder(folder)
     Set folderList = folderObj.SubFolders
     bolFound = False
     For Each item In folderList
          If isTarget(item) Then
             
               deleteTarget = item.ParentFolder.Path
               bolFound = True
                'Log the deletion
               
          Else
               stepThroughTree (item)
          End If
     Next
    If bolFound Then
        'Delete the folder
        logFile.write ("Deleting the folder: " & item.ParentFolder.Path & "...")
        fso.deleteFolder deleteTarget, True
        logFile.writeLine ("OK")
    End If
End Function
0
 

Author Comment

by:jamiekoxford
ID: 8196551
i appreciate what you are saying with that comment, and i will give it a go, but i have two problems with it.

1:  the bolFound routine is called outside the for loop so therefore you cannot reference 'item' as that is a construct of the loop.
2:  I still get the trouble even with the simplist possible call to the fso.deletefolder function as in a simple script like this...
'Create the FileSystem Object
Set fso = CreateObject("Scripting.FileSystemObject")
deleteTarget = "C:\targetfolder\deleteme"

I think you have tried to do things this way because you feel that the scipt is locking the folder that is referenced, but try the little script i have put above... what happens on your box?  on mine, even with an explicit reference (C:\targetfolder\deleteme) it fails.  

Jamie
0
 

Author Comment

by:jamiekoxford
ID: 8196563
sorry, i missed a line off my simple script - it should end like so:

fso.deleteFolder deleteTarget, True
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8196623
Well, in my PC "Internet\History" is deleted without error.
I am running W2K
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8196639
i did a try with "c:\users" path since i haven't a D drive. Anyway, do you have permissions to access that folder?
0
 

Author Comment

by:jamiekoxford
ID: 8196674
im running as admin so i have permissions for all.
i guess 2k is the different factor here so we wont know till i try your changes - can i just confirm...

my script failed for you, but the additions you made, made it work?
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 360 total points
ID: 8196739
"my script failed for you, but the additions you made, made it work? "

yes, as i told you it deletes:"Internet\History" folder
0
 

Expert Comment

by:CleanupPing
ID: 8531880
Hi jamiekoxford,
This old question (QID 20558685) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 8982577
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in a week or two.  I would appreciate any comments by the experts that would help me in making a recommendation.
It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp

GPrentice00
Cleanup Volunteer
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9046131
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Accept Richie_Simonetti's comment as Answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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