Code to look in folder and all Subfolders

Tridiro

Board Regular
Joined
Apr 26, 2007
Messages
97
Hello Everyone,

I have some code (that I did not write) that will list all of the files in a single directory as hyperlinks in a spreed sheet. I am looking to modify the code so it will include all sub directories (no matter how deep). My plan is to build a User Form to toggle sub directories on and off with a checkbox.

This is not all of the code, but just what is relevant.

Code:
Sub HyperlinkFileList()
'Macro purpose:  To create a hyperlinked list of all files in a user
'specified directory, including file size and date last modified
'NOTE:  The 'TextToDisplay' property (of the Hyperlink object) was added
'in Excel 2000.  This code tests the Excel version and does not use the
'Texttodisplay property if using XL 97.

Dim fso As Object, _
    ShellApp As Object, _
    File As Object, _
    SubFolder As Object, _
    Directory As String, _
    Problem As Boolean, _
    ExcelVer As Integer

'Turn off screen flashing
Application.ScreenUpdating = False
Application.DisplayAlerts = False


'Create objects to get a listing of all files in the directory
Set fso = CreateObject("Scripting.FileSystemObject")

'Prompt user to select a directory
Do
    Problem = False
    Set ShellApp = CreateObject("Shell.Application"). _
        Browseforfolder(0, "Please choose a folder", 0, "c:\\")
    
    On Error Resume Next
        'Evaluate if directory is valid
        Directory = ShellApp.self.path
        Set SubFolder = fso.GetFolder(Directory).Files
        If Err.Number <> 0 Then
            If MsgBox("You did not choose a valid directory!" & vbCrLf & _
                "Would you like to try again?", vbYesNoCancel, _
                "Directory Required") <> vbYes Then Exit Sub
            Problem = True
        End If
    On Error GoTo 0
Loop Until Problem = False

Thanks for any help.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello

Here is again code that you did not write, but must understand to make proper usage of it.

Code:
Sub MAIN()

    Make_A_Loop "C:\MyFolder"     'change this to your parent folder name

End Sub

Private Sub Make_A_Loop(sSourceFolder As String)

    With CreateObject("scripting.filesystemobject").Getfolder(sSourceFolder)
    
        For Each fl In .Files
            'here is an example:
            Call MakeHyperlinks(fl.Name)
        Next
    
        For Each SubFolder In .SubFolders
            Make_A_Loop SubFolder.Path
        Next
        
    End With
    
End Sub

The entry point is the procedure "MAIN". The rest is VBA code in an iterative way.

Change the "Call MakeHyperlinks(fl.Name)", I only provided an example of what it might look like.
 
Upvote 0
Thanks, that's what I'm looking for, Not the answer but a push in the right direction.
 
Upvote 0

Forum statistics

Threads
1,224,530
Messages
6,179,373
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top