Macro To List Sub Folders and Files

MarkAn

Board Regular
Joined
Sep 28, 2005
Messages
69
Office Version
  1. 2010
Hi

I am hoping someone can please help me.

I would like to select a Main Folder and the Macro to then list all SubFolders and Files (within the SubFolders), including the date last modified.

Please help.

Thanks in advance

MarkAn
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I have the following code but need to extend it to include the Folder/Subfolder name and date last modified:

Sub GetFileNames()

Dim xRow&, LR&, xDirect$, xFname$, InitialFoldr$

Application.ScreenUpdating = False

LR = Range("B" & Rows.Count).End(xlUp).Row
xRow = 1

InitialFoldr$ = "C:\\" '<<< Startup folder to begin searching from

With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
If ActiveSheet.UsedRange.Find(what:=xFname$) Is Nothing Then
Range("B" & LR).Offset(xRow) = xFname$
xRow = xRow + 1
End If
xFname$ = Dir
Loop
End If
End With
End Sub



I would love it if, the main folder name could be listed in Column A, then in Column B would be any subfolders, with the names of any files in Column C and the date modified in Column D.

If it can't be laid out as above, then any readable layout would be great......

Please help

MarkAn
 
Upvote 0
hi, MarkAn

Some old code I have, from ?? If you cut out unnecessary stuff it'll be faster - 'cause it pulls some slow to get data.

regards

PS. START WITH A NEW WORKBOOK OPEN - just in case it will otherwise overwrite your activeworksheet's data

Rich (BB code):
Option Explicit

Public x()
Public i As Long
Public objShell, objFolder, objFolderItem
Public FSO, oFolder, Fil
Sub MainExtractData()
  Dim NewSht As Worksheet
  Dim MainFolderName As String
  Dim TimeLimit As Long, StartTime As Double
  ReDim x(1 To 65536, 1 To 11)
  Set objShell = CreateObject("Shell.Application")
  TimeLimit = Application.InputBox("Please enter the maximum time that you wish this code to run for in minutes" & vbNewLine & vbNewLine & _
      "Leave this at zero for unlimited runtime", "Time Check box", 0)
  StartTime = Timer
  Application.ScreenUpdating = False
  MainFolderName = BrowseForFolder()
  Set NewSht = ThisWorkbook.Sheets.Add
  x(1, 1) = "Path"
  x(1, 2) = "File Name"
  x(1, 3) = "Last Accessed"
  x(1, 4) = "Last Modified"
  x(1, 5) = "Created"
  x(1, 6) = "Type"
  x(1, 7) = "Size"
  x(1, 8) = "Owner"
  x(1, 9) = "Author"
  x(1, 10) = "Title"
  x(1, 11) = "Comments"
  i = 1
  Set FSO = CreateObject("scripting.FileSystemObject")
  Set oFolder = FSO.GetFolder(MainFolderName)
  'error handling to stop the obscure error that occurs at time when retrieving DateLastAccessed
  On Error Resume Next
  For Each Fil In oFolder.Files
    Set objFolder = objShell.Namespace(oFolder.Path)
    Set objFolderItem = objFolder.ParseName(Fil.Name)
    i = i + 1
    If i Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60 + StartTime) Then
      GoTo FastExit
    End If
    If i Mod 50 = 0 Then
      Application.StatusBar = "Processing File " & i
      DoEvents
    End If
    x(i, 1) = oFolder.Path
    x(i, 2) = Fil.Name
    x(i, 3) = Fil.DateLastAccessed
    x(i, 4) = Fil.DateLastModified
    x(i, 5) = Fil.DateCreated
    x(i, 6) = Fil.Type
    x(i, 7) = Fil.Size
    x(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
    x(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
    x(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
    x(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
  Next
  'Get subdirectories
  If TimeLimit = 0 Then
    Call RecursiveFolder(oFolder, 0)
  Else
    If Timer < (TimeLimit * 60 + StartTime) Then Call RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
  End If
FastExit:
  Range("A:K") = x
  If i < 65535 Then Range(Cells(i + 1, "A"), Cells(65536, "A")).EntireRow.Delete
  Range("A:K").WrapText = False
  Range("A:K").EntireColumn.AutoFit
  Range("1:1").Font.Bold = True
  Rows("2:2").Select
  ActiveWindow.FreezePanes = True
  Range("a1").Activate
  Set FSO = Nothing
  Set objShell = Nothing
  Set oFolder = Nothing
  Set objFolder = Nothing
  Set objFolderItem = Nothing
  Set Fil = Nothing
  Application.StatusBar = ""
  Application.ScreenUpdating = True
End Sub
Sub RecursiveFolder(xFolder, TimeTest As Long)
  Dim SubFld
  For Each SubFld In xFolder.SubFolders
    Set oFolder = FSO.GetFolder(SubFld)
    Set objFolder = objShell.Namespace(SubFld.Path)
    For Each Fil In SubFld.Files
      Set objFolder = objShell.Namespace(oFolder.Path)
      'Problem with objFolder at times
      If Not objFolder Is Nothing Then
        Set objFolderItem = objFolder.ParseName(Fil.Name)
        i = i + 1
        If i Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest Then
          Exit Sub
        End If
        If i Mod 50 = 0 Then
          Application.StatusBar = "Processing File " & i
          DoEvents
        End If
        x(i, 1) = SubFld.Path
        x(i, 2) = Fil.Name
        x(i, 3) = Fil.DateLastAccessed
        x(i, 4) = Fil.DateLastModified
        x(i, 5) = Fil.DateCreated
        x(i, 6) = Fil.Type
        x(i, 7) = Fil.Size
        x(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
        x(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
        x(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
        x(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
        Debug.Print x(i, 1), x(i, 2), x(i, 11)
      Else
        Debug.Print Fil.Path & " " & Fil.Name
      End If
    Next
    Call RecursiveFolder(SubFld, TimeTest)
  Next
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
  'Function purpose:  To Browser for a user selected folder.
  'If the "OpenAt" path is provided, open the browser at that directory
  'NOTE:  If invalid, it will open at the Desktop level
  Dim ShellApp As Object
  'Create a file browser window at the default folder
  Set ShellApp = CreateObject("Shell.Application"). _
      BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
  'Set the folder to that selected.  (On error in case cancelled)
  On Error Resume Next
  BrowseForFolder = ShellApp.self.Path
  On Error GoTo 0
  'Destroy the Shell Application
  Set ShellApp = Nothing
  'Check for invalid or non-entries and send to the Invalid error
  'handler if found
  'Valid selections can begin L: (where L is a letter) or
  '\\ (as in \\servername\sharename.  All others are invalid
  Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
      If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
      If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
      GoTo Invalid
  End Select
  Exit Function
Invalid:
  'If it was determined that the selection was invalid, set to False
  BrowseForFolder = False
End Function
 
Last edited:
Upvote 0
Hi there, as I said before, fantastic code, when running this will look at "My Documents", is there anyway, to amend the code so that any "Shared Document" folders can be included in the pop-up selection?
 
Upvote 0
I don't know. "Shared Document" folders are unknown to me. How do they differ from normal subdirectories?

Can you change the BrowseForFolder function to suit? Maybe comment it out & just enter the path you want?

I'll have a quick google search.
 
Upvote 0
After a google search it seems like something newer than the system I use & I can't help further. cheers
 
Upvote 0
Thanks for this thread it is useful and the Macro I created (OK edited the example) works well for my uses.

One more thing I am looking for is, is it possible to search through shortcut links?

For example, I have a folder with all my project in for several clients, and I have created another folder for one specific client and pasted shotrcut links into that folder for all their projects. This gives me a simple list for their projects and is easy for me. I get a query can I find a document, say number 123-456, the standard windows search doesn't appear to look in shortcut links when searching so is it possible to use something like these examples and do it through excel?

any thanks for any help
 
Upvote 0
Hi Fazza,
Thanks for this code.

One issue working working with it.
Macro listing as #N/A till last row of the Excel file after listing all the files from selected folder.

Can you please check and advise on it.
Thanks a TON!!!
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,841
Members
449,193
Latest member
MikeVol

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