How can I skip a (Sub)Folder in a Scan?

S

SuperJas

Hi

I've tried out Bob Phillip's clever code of creating a list of files within a main folder (and drilling down into all subfolders), pasted below

I am using this to create a list of files within a particular directory (say, N:\Main Dir) to identify files which have been out-of-date and may be archived for space

Unfortunately, I've run into folders that have 'Permission Denied' (Error # 70), tripping up the macro. How can I get the macro to skip to the next folder when it hits this error? I have tried a simple "On Error Resume Next", but it gives weird results (not all files listed, some file names missing etc)

Thanks a million for your help

SuperJas

--------------------------------------------------------

Option Explici

Private Declare Function SHGetPathFromIDList Lib "shell32.dll"
Alias "SHGetPathFromIDListA"
(ByVal pidl As Long,
ByVal pszPath As String) As Lon

Private Declare Function SHBrowseForFolder Lib "shell32.dll"
Alias "SHBrowseForFolderA"
(lpBrowseInfo As BROWSEINFO) As Lon

Private Type BROWSEINF
hOwner As Lon
pidlRoot As Lon
pszDisplayName As Strin
lpszTitle As Strin
ulFlags As Lon
lpfn As Lon
lParam As Lon
iImage As Lon
End Typ

Dim FSO As Objec
Dim cnt As Lon
Dim level As Lon
Dim arFile

Sub Folders(
Dim i As Lon
Dim sFolder As Strin

Set FSO = CreateObject("Scripting.FileSystemObject"

arFiles = Array(
cnt = -
level =

sFolder = GetFolder(
ReDim arFiles(1, 0
If sFolder <> "" The
SelectFiles sFolde
Worksheets.Add.Name = "Files
With ActiveShee
For i = LBound(arFiles, 2) To UBound(arFiles, 2
.Hyperlinks.Add Anchor:=.Cells(i + 1, arFiles(1, i)),
Address:=arFiles(0, i),
TextToDisplay:=arFiles(0, i
Nex
.Columns("A:Z").EntireColumn.AutoFi
End Wit
End I

End Su

'----------------------------------------------------------------------
Sub SelectFiles(ByVal sPath
'----------------------------------------------------------------------
Dim fldr As Objec
Dim Folder As Objec
Dim file As Objec
Dim Files As Objec

Set Folder = FSO.GetFolder(sPath

Set Files = Folder.File
For Each file In File
cnt = cnt +
ReDim Preserve arFiles(1, cnt
arFiles(0, cnt) = Folder.path & "\" & file.Nam
arFiles(1, cnt) = leve
Next fil

level = level +
For Each fldr In Folder.Subfolder
SelectFiles fldr.pat
Nex

End Su

'------------------------------------------------------------
Function GetFolder(Optional ByVal Name As String = "Select a folder."
As Strin
'------------------------------------------------------------
Dim bInfo As BROWSEINF
Dim path As Strin
Dim oDialog As Lon

bInfo.pidlRoot = 0& 'Root folder = Deskto

bInfo.lpszTitle = Nam

bInfo.ulFlags = &H1 'Type of directory to Retur
oDialog = SHBrowseForFolder(bInfo) 'display the dialo

'Parse the resul
path = Space$(512

GetFolder = "
If SHGetPathFromIDList(ByVal oDialog, ByVal path) The
GetFolder = Left(path, InStr(path, Chr$(0)) - 1
End I

End Functio
 
B

Bob Kilmer

On what line of code does the error crop up? I am guessing you need to be
more surgical with your error handling. Catch the specific error in
procedure where it occurs, goto an error handler, clean up, then have the
code restart where it can try to get the *next* folder using Resume <label>
in your error handler.

On Error Goto errhandler
get_folder:
'code gets next folder here
'more code
Exit Sub
errhandler:
'code to clean up, if nec.
Resume get_folder 'go try next.
Ens Sub

SuperJas said:
Hi,

I've tried out Bob Phillip's clever code of creating a list of files
within a main folder (and drilling down into all subfolders), pasted below.
I am using this to create a list of files within a particular directory
(say, N:\Main Dir) to identify files which have been out-of-date and may be
archived for space.
Unfortunately, I've run into folders that have 'Permission Denied' (Error
# 70), tripping up the macro. How can I get the macro to skip to the next
folder when it hits this error? I have tried a simple "On Error Resume
Next", but it gives weird results (not all files listed, some file names
missing etc).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top