I have a folder (C:\DW) which contains several subfolders and image files in each subfolder.
I've searched for example code to return all the image names and populate a list box with them.
I read something about recursive functions ?, i understand the concept but struggling to find code to start me off in this project.
Some examples are for the FileSystemObject some API's using windows hooks .....
I would much prefer FSO as used eleswhere in my project.
Does anyone have a sample of code to provide the basic function as above and return all files ( preferably .JPG ) within the subfolders to start me off ?
Regards
Find all files
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
Find all files
Cheers ...
Dave.
Dave.
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find all files
See Recursive Folder Search - Find Files in Subdirectories - Access Image FAQ for an easy-to-use example.
You need to copy the code for the RecursiveDir and TrailingSlash functions into a standard module.
Here is an example of its use:
where cmdListImages is a command button and lstPictures is a list box. Of course, you can replace the literal path "C:\Pictures" with a variable or control.
You need to copy the code for the RecursiveDir and TrailingSlash functions into a standard module.
Here is an example of its use:
Code: Select all
Private Sub cmdListImage_Click()
Dim colFiles As New Collection
RecursiveDir colFiles, "C:\Pictures", "*.jpg", True
Dim vFile As Variant
For Each vFile In colFiles
Me.lstPictures.AddItem vFile
Next vFile
End Sub
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
Re: Find all files
Hi Hans , just wondering how to trap the Z drive if it doesn't exist ?
Sometimes the card reader (Z) drops and loads the cmnDialog instead ! Where would the best place be to capture this? I have the code, just need assurance as to the best place....
Regards
Sometimes the card reader (Z) drops and loads the cmnDialog instead ! Where would the best place be to capture this? I have the code, just need assurance as to the best place....
Regards
Cheers ...
Dave.
Dave.
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find all files
Try this, somewhere at the beginning of your code, before calling RecursiveDir:
Code: Select all
Dim strTest As String
On Error Resume Next
strTest = Dir("Z:")
If Err = 52 Then
MsgBox "Drive not available", vbExclamation
Exit Sub
End If
On Error GoTo 0
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
Re: Find all files
Hi Hans
I did this another way.
If Z:\ does not exist then load the cmnDialog with the path of "MyComputer".
If Z:\ Does exist then load all images recursively throughout the drive and subfolders.
The idea being the Z drive is an SD card from a camera. If the user has taken photos and puts the card in the Z drive, the photo's are loaded, but if no card is present then the dialog opens allowing the user to navigate ("My Computer") and select his own.
Here's the code:
' Form Open Event:
Private Sub Form_Load()
Dim strCameraFolder As String
strCameraFolder = "Z:\"
If Dir(strCameraFolder, vbDirectory) = "" Then
Call cmdOpen_Click
Else
Dim colFiles As New Collection
RecursiveDir colFiles, "Z:\", "*.jpg", True
Dim vFile As Variant
For Each vFile In colFiles
Me.lstImages.AddItem vFile
Next vFile
End If
End Sub
' cmdOpen Code:
Private Sub cmdOpen_Click()
Const MYCOMPUTER As String = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
On Error GoTo cError
Dim i As Integer
Dim myFiles() As String
Dim myPath As String
ClearList ' Clearlist function clears the listbox
With CmnDialog1
.MaxFileSize = 32000 'this will max out the buffer for the filenames array for large selections. *NEW*
.CancelError = False 'if cancel is pressed, the code jumps to cError because of the On Error statement above
.Filter = "JPG Files (*.jpg)|*.jpg"
.FilterIndex = 2
.FileName = ""
.InitDir = MYCOMPUTER
.Flags = CD_FLAGS 'this is where we tell it to use multiselect
.ShowOpen
myFiles = Split(.FileName, vbNullChar) 'the Filename returned is delimeted by a null character because we selected the cdlOFNLongNames flag
Select Case UBound(myFiles)
Case 0 'if only one was selected we are done
lstImages.AddItem myFiles(0)
Case Is > 0 'if more than one, we need to loop through it and append the root directory
For i = 1 To UBound(myFiles)
myPath = myFiles(0) & IIf(Right(myFiles(0), 1) <> "\", "\", "") & myFiles(i)
lstImages.AddItem myPath
Next i
End Select
Me.txtFileCount.Text = "Total Images In Folder" & " " & Me.lstImages.ListCount
End With
Exit Sub
cError:
Beep
MsgBox Err.Description '*NEW*
End Sub
This seems to suit my purpose better and guides the user either way to collect his images.
Just one area to cover, if Z exists but there are no images on the drive ( using recursive ) can I launch cmdOpen, if no images exist in the collection then it's pointless loading Z ?
Regards
I did this another way.
If Z:\ does not exist then load the cmnDialog with the path of "MyComputer".
If Z:\ Does exist then load all images recursively throughout the drive and subfolders.
The idea being the Z drive is an SD card from a camera. If the user has taken photos and puts the card in the Z drive, the photo's are loaded, but if no card is present then the dialog opens allowing the user to navigate ("My Computer") and select his own.
Here's the code:
' Form Open Event:
Private Sub Form_Load()
Dim strCameraFolder As String
strCameraFolder = "Z:\"
If Dir(strCameraFolder, vbDirectory) = "" Then
Call cmdOpen_Click
Else
Dim colFiles As New Collection
RecursiveDir colFiles, "Z:\", "*.jpg", True
Dim vFile As Variant
For Each vFile In colFiles
Me.lstImages.AddItem vFile
Next vFile
End If
End Sub
' cmdOpen Code:
Private Sub cmdOpen_Click()
Const MYCOMPUTER As String = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
On Error GoTo cError
Dim i As Integer
Dim myFiles() As String
Dim myPath As String
ClearList ' Clearlist function clears the listbox
With CmnDialog1
.MaxFileSize = 32000 'this will max out the buffer for the filenames array for large selections. *NEW*
.CancelError = False 'if cancel is pressed, the code jumps to cError because of the On Error statement above
.Filter = "JPG Files (*.jpg)|*.jpg"
.FilterIndex = 2
.FileName = ""
.InitDir = MYCOMPUTER
.Flags = CD_FLAGS 'this is where we tell it to use multiselect
.ShowOpen
myFiles = Split(.FileName, vbNullChar) 'the Filename returned is delimeted by a null character because we selected the cdlOFNLongNames flag
Select Case UBound(myFiles)
Case 0 'if only one was selected we are done
lstImages.AddItem myFiles(0)
Case Is > 0 'if more than one, we need to loop through it and append the root directory
For i = 1 To UBound(myFiles)
myPath = myFiles(0) & IIf(Right(myFiles(0), 1) <> "\", "\", "") & myFiles(i)
lstImages.AddItem myPath
Next i
End Select
Me.txtFileCount.Text = "Total Images In Folder" & " " & Me.lstImages.ListCount
End With
Exit Sub
cError:
Beep
MsgBox Err.Description '*NEW*
End Sub
This seems to suit my purpose better and guides the user either way to collect his images.
Just one area to cover, if Z exists but there are no images on the drive ( using recursive ) can I launch cmdOpen, if no images exist in the collection then it's pointless loading Z ?
Regards
Cheers ...
Dave.
Dave.
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find all files
You could use something like this:
Code: Select all
' Form Open Event:
Private Sub Form_Load()
Dim strCameraFolder As String
strCameraFolder = "Z:\"
If Dir(strCameraFolder, vbDirectory) = "" Then
Call cmdOpen_Click
Else
Dim colFiles As New Collection
RecursiveDir colFiles, "Z:\", "*.jpg", True
If colFiles.Count > 0 Then
Dim vFile As Variant
For Each vFile In colFiles
Me.lstImages.AddItem vFile
Next vFile
Else
Call cmdOpen_Click
End If
End If
End Sub
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
Re: Find all files
Hans
I had to include the error 52 code .. seemed to cause a problem on one of the machines.
Have I applied it correctly? :
Private Sub Form_Load()
On Error Resume Next
Dim strCameraFolder As String
strCameraFolder = "Z:\"
If Dir(strCameraFolder, vbDirectory) = "" Then
Call cmdOpen_Click
Else
Dim colFiles As New Collection
RecursiveDir colFiles, "Z:\", "*.jpg", True
If colFiles.Count > 0 Then
Dim vFile As Variant
For Each vFile In colFiles
Me.lstImages.AddItem vFile
Next vFile
Else
Call cmdOpen_Click
End If
End If
If Error = 52 Then
Call cmdOpen_Click
Exit Sub
End If
On Error GoTo 0
End Sub
I had to include the error 52 code .. seemed to cause a problem on one of the machines.
Have I applied it correctly? :
Private Sub Form_Load()
On Error Resume Next
Dim strCameraFolder As String
strCameraFolder = "Z:\"
If Dir(strCameraFolder, vbDirectory) = "" Then
Call cmdOpen_Click
Else
Dim colFiles As New Collection
RecursiveDir colFiles, "Z:\", "*.jpg", True
If colFiles.Count > 0 Then
Dim vFile As Variant
For Each vFile In colFiles
Me.lstImages.AddItem vFile
Next vFile
Else
Call cmdOpen_Click
End If
End If
If Error = 52 Then
Call cmdOpen_Click
Exit Sub
End If
On Error GoTo 0
End Sub
Cheers ...
Dave.
Dave.
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find all files
I'd use
Code: Select all
Private Sub Form_Load()
Dim strCameraFolder As String
Dim colFiles As New Collection
On Error GoTo ErrHandler
strCameraFolder = "Z:\"
If Dir(strCameraFolder, vbDirectory) = "" Then
Call cmdOpen_Click
Else
RecursiveDir colFiles, "Z:\", "*.jpg", True
If colFiles.Count > 0 Then
Dim vFile As Variant
For Each vFile In colFiles
Me.lstImages.AddItem vFile
Next vFile
Else
Call cmdOpen_Click
End If
End If
Exit Sub
ErrHandler:
If Err = 52 Then
Call cmdOpen_Click
End If
End Sub
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England