Permission Denied Error - 800A0046

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jsuttorAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.