Microsoft VBScript runtime error Code 800A0046 line 49 , Char 1 "Permission denied"

Hi Experts,

I'm using a script to clear Temp files and folders developed by Michael Harris and posted at:

https://groups.google.com/forum/#!topic/microsoft.public.scripting.wsh/j7e2oL0F0pY

It has worked for me since I re-installed Windows 7 a couple of weeks ago but has suddenly started giving me this error:

Microsoft VBScript runtime error Code 800A0046 line 49 , Char 1 "Permission denied"

(Note: I had actually had this happen to me a few years back, so I don't know if this is an intermittent problem or what conditions make it happen)

This is the code (Note, too that I get the same error when I try to open the script in Microsoft Windows Based Script Host, so this was opened with Notepad):

'===DeleteTempFiles.vbs===
' Source: http://groups.google.com/group/microsoft.public.scripting.wsh/browse_frm/thread/8fb7b6a0bd05d296/eb34633c642bcdb7#eb34633c642bcdb7
'Michael Harris
'Microsoft.MVP.Scripting

Const TemporaryFolder = 2 'for GetSpecialFolder
set fso = createobject("scripting.filesystemobject")
'init an empty array (ubound will be -1)...
'
'we use an array and store the file objects.
'this avoids any problems with altering the
'contents of the Files collections while they
'are being iterated.
'
arFiles = array()
count = -1
'get the path to the temp folder
'
tempdir = fso.GetSpecialFolder(TemporaryFolder)
'load the (global scope) arFiles
'SelectFiles calls itself recursively
'for SubFolders
'
SelectFiles tempdir
msgbox count+1 & " files found"
'now do the actual deletes. the error trap
'is in case any are in-use...
'
dcount = 0
for each file in arFiles
on error resume next
file.delete true
if err.number = 0 then dcount = dcount + 1
err.clear
on error goto 0
next
'now go back and delete empty folders
'below the temp folder
DeleteEmptyFolders tempdir,false
'comment out for "silent" operation,
'or add support for a "/s" command-line switch.
'
msgbox count+1 & " files found, " & dcount & " deleted."
sub SelectFiles(sPath)
'select files to delete and add to array...
'
set folder = fso.getfolder(sPath)
set files = folder.files
for each file in files
count = count + 1
redim preserve arFiles(count)
set arFiles(count) = file
next
for each fldr in folder.subfolders
SelectFiles fldr.path
next
end sub
sub DeleteEmptyFolders(sPath,bDeleteThisFolder)
set folder = fso.getfolder(sPath)
'recurse first...
'
for each fldr in folder.subfolders
DeleteEmptyFolders fldr.path,true
next
'if no files or folders then delete...
'
'bDeleteThisFolder is false for
'the root of the subtree, and true for
'sub-folders (unless you want to delete
'the entire subtree if it is empty).
'
if (folder.files.count = 0) and _
(folder.subfolders.count) = 0 and _
bDeleteThisFolder then
folder.delete
exit sub
end if
end sub
'
'===end-script===

Open in new window


I'd appreciate any insights.

Jeff
LVL 2
Jeffrey SmithOwnerAsked:
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.

Bill PrewCommented:
The script really needs some error trapping logic to prevent it from failing in cases like this, but to see what we are up against, add the following line near the top of the indicated subroutine.  Then run the script from a command prompt using CSCRIPT, and let's wee what the last path it displays before the error occurs.

sub SelectFiles(sPath)
'select files to delete and add to array...
'
wscript.echo "SelectFiles: """ & sPath & """"
set folder = fso.getfolder(sPath)

Open in new window

~bp
0
Bill PrewCommented:
Here's a modified version that adds some error checking to prevent hard halts in the script if files or folders are not accessible.  Give it a test and see if it helps.

~bp
0
Jeffrey SmithOwnerAuthor Commented:
Thanks for posting, Bill.  I'm thinking you meant to attach a file to your last post, but I'm not seeing it ...

Jeff
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Bill PrewCommented:
Crud, I hate when I do that, which thankfully isn't too often.  Unfortunately I don't think I saved it so may have to redo, but will do that...

~bp
0
Jeffrey SmithOwnerAuthor Commented:
Thanks, Bill.  I know the pain ... ;--)
0
Bill PrewCommented:
Okay, I think is close to what i had.

'===DeleteTempFiles.vbs===
' Source: http://groups.google.com/group/microsoft.public.scripting.wsh/browse_frm/thread/8fb7b6a0bd05d296/eb34633c642bcdb7#eb34633c642bcdb7
'Michael Harris
'Microsoft.MVP.Scripting

Const TemporaryFolder = 2 'for GetSpecialFolder
Set fso = CreateObject("scripting.filesystemobject")

'init an empty array (ubound will be -1)...
'we use an array and store the file objects.
'this avoids any problems with altering the
'contents of the Files collections while they
'are being iterated.
arFiles = Array()
Count = -1

'get the path to the temp folder
tempdir = fso.GetSpecialFolder(TemporaryFolder)

'load the (global scope) arFiles
'SelectFiles calls itself recursively
'for SubFolders
SelectFiles tempdir
MsgBox Count + 1 & " files found"

'now do the actual deletes. the error trap
'is in case any are in-use...
dcount = 0

For Each file in arFiles
    On Error Resume Next
    file.delete True
    If Err.Number = 0 Then dcount = dcount + 1
    Err.Clear
    On Error goto 0
Next

'now go back and delete empty folders
'below the temp folder
DeleteEmptyFolders tempdir, False

'comment out for "silent" operation,
'or add support for a "/s" command-line switch.
MsgBox Count + 1 & " files found, " & dcount & " deleted."

Sub SelectFiles(sPath)
    On Error Resume Next

    'select files to delete and add to array...
    Set folder = fso.getfolder(sPath)
    Set files = folder.files

    For Each file in files
        Count = Count + 1
        ReDim preserve arFiles(Count)
        Set arFiles(Count) = file
    Next

    If Err.Number = 0 Then
        For Each fldr in folder.subfolders
            SelectFiles fldr.path
        Next
    End If
End Sub

Sub DeleteEmptyFolders(sPath, bDeleteThisFolder)
    On Error Resume Next

    Set folder = fso.getfolder(sPath)

    If Err.Number = 0 Then
        'recurse first...
        For Each fldr in folder.subfolders
            DeleteEmptyFolders fldr.path, True
        Next
    End If

    'if no files or folders then delete...
    '
    'bDeleteThisFolder is false for
    'the root of the subtree, and true for
    'sub-folders (unless you want to delete
    'the entire subtree if it is empty).
    If Err.Number = 0 Then
        If (folder.files.Count = 0) And (folder.subfolders.Count) = 0 And bDeleteThisFolder Then
            folder.delete
            Exit Sub
        End If
    End If
End Sub

Open in new window

~bp
0

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
Jeffrey SmithOwnerAuthor Commented:
Thanks, Bill !

That seems to be doing the job !

Jeff
0
Bill PrewCommented:
Welcome Jeff.

~bp
0
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
VB Script

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.