Permission Denied Error - 800A0046

Posted on 2006-04-26
Last Modified: 2008-01-09
OK I have this script I am using to delete files from a directory and when I run it I get this VBScript Error... 800A0046 Permission Denied...  I have tried running it as the Administrator, I have tried giving everyone Full Control to the director but I can not seem to get this script to get past this. Can someone give me some suggestions.

Below is the first part of the code that executes the file delete and where I am getting the error. The error happens on the deleting of the files.


Set objFolder = objFSO.GetFolder("C:\SportsTicker\xmlfiles\")
Set filesys = CreateObject("Scripting.FileSystemObject")
For Each file in objFolder.Files
Set objTextStream = objFSO.OpenTextFile(objFolder & "\" & file.Name)
fname = left(,11)
fnamelog = right(,9)
if ucase(fnamelog) = ucase("inj-r.xml") or ucase(fnamelog) = ucase("log-r.xml") or ucase(fnamelog) = ucase("ros-r.xml") then
set demofile = filesys.GetFile(objFolder & "\" & file.Name)
demofile.Delete (True)

Maybe my code is wrong and the error is just because the files don't exist. So here is what I am trying to do. I am trying to delete all the files that have inj-r.xml , ros-r.xml and log-r.xml as the last 9 characters of the file name.
Question by:jsuttor
    LVL 7

    Expert Comment

    My first guess would be that you can't delete because the file is being held by your loop.  This would prevent deletion because the file is still open.

    LVL 7

    Accepted Solution

    I'm not exactly sure what you need to do, but can you change the code to this...  I'm assuming objFSO is a FileSystemObject

    Set objFolder = objFSO.GetFolder("C:\SportsTicker\xmlfiles\")
    For Each file in objFolder.Files
       fname = left(,11)
       if ucase(fnamelog=ucase("inj-r.xml") or ucase(fnamelog) = ucase("log-r.xml") or ucase(fnamelog) = ucase("ros-r.xml") then
            ' Add this if you need to open it.
            Set objTextStream = objFSO.OpenTextFile(objFolder & "\" & file.Name)
       end if
    LVL 3

    Author Comment

    You were right... the file was being kept open by the loop.. Second I moved the Set objTextStream = objFSO.OpenTextFile(objFolder & "\" & file.Name) to the elseif in my document everything was perfect.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    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…
    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…

    734 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

    17 Experts available now in Live!

    Get 1:1 Help Now