Re: wildacard character
- From: "Allen Browne" <AllenBrowne@xxxxxxxxxxxxxx>
- Date: Tue, 20 Jun 2006 11:30:16 +0800
The example below shows how to list files in a folder and subfolders:
Public Function ListFiles(strPath As String, _
Optional strFileSpec As String, _
Optional bIncludeSubfolders As Boolean)
On Error GoTo Err_Handler
'Purpose: List the files in the path.
'Arguments: strPath = the path to search.
' strFileSpec = "*.*" unless you specify differently.
' bIncludeSubfolders: If True, returns results from
subdirectories of strPath as well.
'Method: FilDir() adds items to a collection, calling itself
recursively for subfolders.
Dim colDirList As New Collection
Dim vItem As Variant
Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
'List the items. (Or, you could add them to a list box.)
For Each vItem In colDirList
Debug.Print vItem
Next
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
End Function
Private Function FillDir(colDirList As Collection, _
ByVal strFolder As String, _
strFileSpec As String, _
bIncludeSubfolders As Boolean)
'Build up a list of files, and then add add to this list, any additional
folders
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add the files to the folder.
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colDirList.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Build collection of additional subfolders.
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop
'Call function recursively for each subfolder.
For Each vFolderName In colFolders
Call FillDir(colDirList, strFolder & TrailingSlash(vFolderName),
strFileSpec, True)
Next vFolderName
End If
End Function
Private Function TrailingSlash(varIn As Variant) As String
If Len(varIn) > 0& Then
If Right(varIn, 1&) = "\" Then
TrailingSlash = varIn
Else
TrailingSlash = varIn & "\"
End If
End If
End Function
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Phil" <Phil@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:CC5BD465-EACF-429B-818B-F4C6569617F4@xxxxxxxxxxxxxxxx
Hello again,
What could I use to perform the following.
I have a CD with data on it except its not all in one directory
eg:
E:\Pictures\021\
E:\Pictures\022\
E:\Pictures\023\
E:\Pictures\054\
The problem is that I have many CD's with at pictures with various sub
directories.
Is there a wildcard character that I could use to substitite for the
folder
name?
Thanks
--
Phil
.
- Follow-Ups:
- Re: wildacard character
- From: Phil
- Re: wildacard character
- Prev by Date: Re: Forms Help Please......
- Next by Date: Re: Need to create a 14 ages form.
- Previous by thread: Re: How do I code for a button in one form to do the funtion in anothe
- Next by thread: Re: wildacard character
- Index(es):