listing folder names and sub files

Yuriy B

New Member
Joined
Feb 4, 2015
Messages
26
Hi,

Is it possible for excel to list folder names and the contents of all subfolders in excel?

Just like the RUN command that generates the text file list: dir /s >listmyfolder.txt

Thanks
 
Got it to work properly with the following code:

Code:
'---------
Public Sub ScanSubfolders()
'---------
Dim FileSystem As Object
Dim HostFolder As String




Range("A1").Value = "Drive"
Range("b1").Value = "Subfolder 1"
Range("c1").Value = "Subfolder 2"
Range("d1").Value = "Subfolder 3"
Range("e1").Value = "Subfolder 4"
Range("f1").Value = "Subfolder 5"
Range("g1").Value = "File Name"
Range("h1").Value = "Modify Date"




Range("A2").Select
HostFolder = "H:"








Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub


'---------
Private Sub DoFolder(Folder)
'---------
Dim SubFolder
Dim oFile
Dim LArray() As String					'SET LArray AS STRING
    On Error Resume Next				'IGNORE ALL ERRORS
  
 For Each SubFolder In Folder.SubFolders
           'Debug.Print SubFolder
        DoFolder SubFolder
    Next
    
    For Each oFile In Folder.Files
            ' Operate on each file
       ' Debug.Print Folder, oFile.Name, getFileProperty(oFile, 5)
         If InStr(oFile, ".xls") > 0 Then
         LArray() = Split(Folder, "\")			    'USE SPLIT FUNCTION TO SEPORATE BY DEFINED DELIMITER (\)
            ActiveCell.Value = LArray()                        'DISPLAY DRIVE LETTER ONLY
            ActiveCell.Offset(0, 1).Value = LArray(1)              'DISPLAY NAME OF SUBFOLDER 1
            ActiveCell.Offset(0, 2).Value = LArray(2)                  'DISPLAY NAME OF SUBFOLDER 2
            ActiveCell.Offset(0, 3).Value = LArray(3)                      'DISPLAY NAME OF SUBFOLDER 3
            ActiveCell.Offset(0, 4).Value = LArray(4)                         'DISPLAY NAME OF SUBFOLDER 4
            ActiveCell.Offset(0, 5).Value = LArray(5)                             'DISPLAY NAME OF SUBFOLDER 5
            ActiveCell.Offset(0, 6).Value = oFile.Name                                'DISPLAY NAME OF FILE
            ActiveCell.Offset(0, 7).Value = oFile.DateLastModified                       'DISPLAY FILE MODIFY DATE
            ActiveCell.Offset(1, 0).Select   'next row				             'REPEAT THE PROCESS ONE ROW DOWN
       End If
    Next
End Sub
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,215,366
Messages
6,124,516
Members
449,168
Latest member
CheerfulWalker

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