Link to home
Start Free TrialLog in
Avatar of jamiekoxford
jamiekoxford

asked on

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

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
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

It appears that you are trying to delete when it is in use.
Avatar of jamiekoxford
jamiekoxford

ASKER

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?
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?
i got the same error as you but the folder "History" is deleted anyway.
It doesnt delete on my system unfortunately.  This is a well known error.  There must be a solution!
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
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
sorry, i missed a line off my simple script - it should end like so:

fso.deleteFolder deleteTarget, True
Well, in my PC "Internet\History" is deleted without error.
I am running W2K
i did a try with "c:\users" path since i haven't a D drive. Anyway, do you have permissions to access that folder?
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?
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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:
https://www.experts-exchange.com/help/closing.jsp

GPrentice00
Cleanup Volunteer
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