Permission Denied Error - 800A0046

Posted on 2006-04-26
Medium Priority
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(file.name,11)
fnamelog = right(file.name,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
  • 2

Expert Comment

ID: 16546734
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.


Accepted Solution

pauljk1619 earned 2000 total points
ID: 16546779
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(file.name,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

Author Comment

ID: 16546949
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

807 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