Thanks for your suggestions.
Do you mean, in what you wrote, that NOTHING was listed in the
Immediate Window? If so, then it seems the error routine was never
triggered on your machine, even though there clearly were run-time
errors.
While nothing was listed in the immediate window, stepping through the
code did trap errors that occured but your error handler dismissed
them:
[If Error < ""] Not sure what that means, exactly!
The folders I listed in my post were what was displayed in StatusBar.
I do use option explicit and the variables are Dim'd at a different
level. The "re" code is regular expression stuff to filter to the
appropriate name, and that works OK.
Alias and System are system variables that indicate those particular
attributes. System = 4 and Alias = 1024. Probably they aren't
available with late binding.
If subfldr is a Windows 7 junction, then the Alias attribute will be
set.
I was using those to filter out some of the folders that did not need
to be checked.
I, too, had no problem with similar code running under XP SP3. But
that OS did not have this UAC.
I changed my code to remove the "and System" and "and Alias"
comparisons as you did; and the only change was I got my failure at a
different point:
Immediate Window:
Permission denied C:\$Recycle.Bin\S-1-5-20
Permission denied C:\$Recycle.Bin
then got run-time error while processing
C:\MSOCache
I also changed my code to use late binding as you have above, and that
did not make any difference.
In my original code, the line in the immediate window:
Permission denied C:\MSOCache
and I then get the run-time error while processing
C:\PerfLogs
If I start Excel running "As Administrator", then the code seems to
complete; and there are no messages written in the Immediate window.
Here's the entire code:
=====================================
'Need to set reference to
' Microsoft VBScript Regular Expressions 5.5
' Microsoft Scripting Runtime
Option Explicit
Dim FSO As FileSystemObject
Dim Fldrs As Folders, Fld As Folder
Dim FLS As Files, F As File
Dim re As RegExp
Dim i As Long
Const sPat As String = "^.*\.[^.~]+~[^.~]+$"
Sub RemoveTildeFiles()
i = 1
Set FSO = New FileSystemObject
Set Fld = FSO.GetFolder("C:\")
Set FLS = Fld.Files
Cells.ClearContents
Set re = New RegExp
re.Pattern = sPat
ShowSubFolders (Fld)
Application.StatusBar = False
End Sub
Sub ShowSubFolders(sFldr As String)
Dim subFldrs As Folders, subFldr As Folder
Set subFldrs = FSO.GetFolder(sFldr).subFolders
For Each subFldr In subFldrs
If Not subFldr.Attributes And Alias Then
Application.StatusBar = "Processing folder: " & subFldr
If Not subFldr.Attributes And System Then
On Error GoTo PermissionDenied
Set FLS = subFldr.Files
For Each F In FLS
If re.Test(F.Name) = True Then
Cells(i, 1).Value = F.Path
i = i + 1
End If
Next F
ShowSubFolders (subFldr)
End If
End If
PermissionDenied: If Error < "" Then Debug.Print Error, subFldr
On Error GoTo 0
Next subFldr
End Sub
===============================
If I copy/paste your code into my Excel, and add a calling sub:
====================
Sub ssf()
ShowSubFolders ("C:\")
End Sub
==================
I get a permission denied Run-time error '70' while processing
C:\Documents and Settings\All Users\Desktop
which is a junction.
If I uncomment the line that checks for that, and change Alias to
1024, I get the run-time permission denied error while processing
C:\MSOcache.
==========================
So, neither your code nor mine seems to work on my machine. And it
seems to have something to do with the VBA On Error routine not
trapping all of the Permission Denied errors.
In reviewing what I tested, I may have over stepped the last error,
which ended the proc prematurely. I did get a rather substantial
listing on the spreadsheet, but due to recursive activity the list did
not clear before the counter was re-initialized. This caused previous
entries to be overwritten. I wonder if the counter should be declared
Static so the list on the spreadsheet is contiguous and correctly
reflects the results in the same order as executed.
However, and based in particular that you took the trouble to test
very similar code on your W7 machine, and it worked as designed, I
changed my error handling routine to put it outside the main body of
the Sub:
=========================
.
.
.
ShowSubFolders (subFldr)
End If
End If
NextSubFolder: Next subFldr
Exit Sub
PermissionDenied:
Debug.Print Error, subFldr
Resume NextSubFolder
End Sub
=============================
This seems to work OK, with a long list of files listed in the
Immediate Window (most from C:\Windows.old\Windows)
So I've got this routine working at this level.
But I wonder why the On Error routine seems to behave differently on
your machine than mine;
This may not be the case as per my overstep mentioned.
and why having the On Error routine in the
body of the Sub seems to disable the error handler after the first or
second trap, whereas it does not when entered in the more usual
fashion.
But thank you very much for trying things on your machine. It was very
helpful.
No problem! I don't mind spending the time because I find this
interesting; ..I'm hoping to learn more about how to construct
recursive procs. Now that you've posted the entire code, I can re-test
this and report back.
--
Garry
Free usenet access at
http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc