Solved List Files Recursively (1 Viewer)

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292

Hi all,

I am after some help with the above link. I am after a way of displaying all items inside a folder. The current code I have shows only the top level folder contents and no subfolders.

The above link shows all files, which is great, but it displays the full path (S:\mainfolder\subfolder\filename.ext), including the top level folder. My listbox is not wide enough to show the entire path and I do not have the space to make it wide enough.

Would anyone know how to adjust this to remove the main folder path and just show as subfolder\filename.ext

Regards,
Matt
 

cheekybuddha

AWF VIP
Local time
Today, 10:13
Joined
Jul 21, 2014
Messages
2,237
In the ListFiles function try amending to:
Code:
' ...
    If lst Is Nothing Then
        For Each varItem In colDirList
            Debug.Print varItem
        Next
    Else
        For Each varItem In colDirList
            lst.AddItem Mid(varItem, InStrRev(varItem, "\") + 1)
        Next
    End If
' ...
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292
In the ListFiles function try amending to:
Code:
' ...
    If lst Is Nothing Then
        For Each varItem In colDirList
            Debug.Print varItem
        Next
    Else
        For Each varItem In colDirList
            lst.AddItem Mid(varItem, InStrRev(varItem, "\") + 1)
        Next
    End If
' ...
I shall give that a go. Thank you

~Matt
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292
In the ListFiles function try amending to:
Code:
' ...
    If lst Is Nothing Then
        For Each varItem In colDirList
            Debug.Print varItem
        Next
    Else
        For Each varItem In colDirList
            lst.AddItem Mid(varItem, InStrRev(varItem, "\") + 1)
        Next
    End If
' ...
Thanks Cheeky,

This code does kind of work. It now shows all the files in all the folders, but doesn't show any folders at all. I think this will be fine though as it does still list all files.

Thank you for your help.

~Matt
 

cheekybuddha

AWF VIP
Local time
Today, 10:13
Joined
Jul 21, 2014
Messages
2,237
Perhaps try adjusting like:
Code:
' ...
    Dim colDirList As New Collection
    Dim varItem As Variant
    Dim arrSplit As Variant, iUbound As Integer, i As Integer, FldrFile As String
    
    Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
    
    'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
    If lst Is Nothing Then
        For Each varItem In colDirList
            Debug.Print varItem
        Next
    Else
        For Each varItem In colDirList
          arrSplit = Split(varItem, "\")
          iUbound = UBound(arrSplit)
          If iUbound > 0 Then
            FldrFile = arrSplit(iUbound -1) & "\" & arrSplit(iUbound)
          Else
            FldrFile = arrSplit(iUbound)
          End If
          lst.AddItem FldrFile
        Next
    End If
' ...
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292
Perhaps try adjusting like:
Code:
' ...
    Dim colDirList As New Collection
    Dim varItem As Variant
    Dim arrSplit As Variant, iUbound As Integer, i As Integer, FldrFile As String
   
    Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
   
    'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
    If lst Is Nothing Then
        For Each varItem In colDirList
            Debug.Print varItem
        Next
    Else
        For Each varItem In colDirList
          arrSplit = Split(varItem, "\")
          iUbound = UBound(arrSplit)
          If iUbound > 0 Then
            FldrFile = arrSplit(iUbound -1) & "\" & arrSplit(iUbound)
          Else
            FldrFile = arrSplit(iUbound)
          End If
          lst.AddItem FldrFile
        Next
    End If
' ...
Thank you David,

It works perfect. You are the Lionel Messi of VBA

~Matt
 

cheekybuddha

AWF VIP
Local time
Today, 10:13
Joined
Jul 21, 2014
Messages
2,237
If you need explanation of what's happening in the VBA, please ask.

(y)

d
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:13
Joined
May 21, 2018
Messages
8,463
Working application with treeview if desired.
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292
If you need explanation of what's happening in the VBA, please ask.

(y)

d
Thanks David,

Once I see the code, I can understand it. It's just the figuring it out first. I only do this in little gaps in my normal job so everything is learn as I go.

As soon as I get time to see working code, I can break it down in my head. I wish I had time to do this full time. I could make some wonderful things given the right time.

~Matt
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292
Working application with treeview if desired.
Thanks Maj,

I did see this and it looks wonderful, but far more advanced than we would need here.

The file list bottom right is perfect for what we need. They click the Folder button to access the folder.

If there is no folder created, my code will create the folder on the shared network drive and open to allow them to uploaded the files.

~Matt

1632927513629.png
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 10:13
Joined
Feb 5, 2019
Messages
292
Not sure what to say. I stripped the code down to the basics to do any recursion of a folder. Which part is too complex?
Code:
Private FSO As FileSystemObject
Public Sub InitializeSpan()
  Set FSO = New FileSystemObject
  SpanFolders GetDefaultFolderPath  ' Need to pass in the startfolder. GetDefaultFolderPath is a custom function
  Set FSO = Nothing
End Sub

Private Sub SpanFolders(SourceFolderFullName As String)

    Dim SourceFolder As Scripting.Folder 'Scripting.Folder
    Dim SubFolder As Scripting.Folder 'Scripting.Folder
    Dim FileItem As Scripting.file 'Scripting.File
 
    Set SourceFolder = FSO.GetFolder(SourceFolderFullName)
 
       For Each FileItem In SourceFolder.Files
        'Custom method to save these properties to a table
        ' LogFilesFolders FileItem.Name, FileItem.Path, FileItem.Type, ParentID, fft_File, FolderLevel
      Next FileItem
 
    For Each SubFolder In SourceFolder.SubFolders
     'Another custom method to save folder information
     ' LogFilesFolders SubFolder.Name, SubFolder.Path, SubFolder.Type, ParentID, fft_Folder, FolderLevel
     'Here is the recursive Call
      SpanFolders SubFolder.Path, ParentID, FolderLevel
    Next SubFolder

    Set FileItem = Nothing
    Set SourceFolder = Nothing

End Su
Sorry Maj,

Not that the code is too complicated. Just that the people here literally just want a list of the files in front of them as per my screenshot above.

Your tree method is very good, but some folders may only have 1 subfolder and 2 files. Having them as a pure list is just easier for the users here.

If there was to be a large amount of files, folders and subfolders, your tree would be the way I would go and teach them here.

For what we have here, the list as I have now got it will be fine.

~Matt
 

Users who are viewing this thread

Top Bottom