Advertisement
Nihad

VBA Code: Get File Name and Path

Apr 22nd, 2024 (edited)
1,148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.14 KB | Source Code | 0 0
  1. Option Explicit
  2.  
  3. Public Sub ShowFilePaths()
  4.     Dim rootFolder As String
  5.     rootFolder = SelectFolder
  6.     If rootFolder = vbNullString Then Exit Sub
  7.  
  8.  
  9.     rootFolder = rootFolder & IIf(Right$(rootFolder, 1) = "\", vbNullString, "\")
  10.  
  11.     Dim pathArray As Variant
  12.     pathArray = GetAllFiles(rootFolder)
  13.  
  14.     Dim folderGroups As Object
  15.     Set folderGroups = BuildFolderDictionary(rootFolder, pathArray)
  16.  
  17.  
  18.     On Error Resume Next
  19.     With Sheet1
  20.         .UsedRange.ClearOutline
  21.         .UsedRange.Clear
  22.         .Outline.SummaryRow = xlAbove
  23.     End With
  24.     Err.Clear
  25.     On Error GoTo 0
  26.  
  27.  
  28.     Const START_ROW As Long = 6
  29.     Dim pathRange As Range
  30.     Set pathRange = Sheet1.Range("A" & START_ROW).Resize(UBound(pathArray), 1)
  31.     pathRange = pathArray
  32.  
  33.  
  34.     '       and group the same rows
  35.     Const MAX_GROUP_LEVEL As Long = 8
  36.     Dim rowGroup As Variant
  37.     Dim level As Long
  38.     Dim folderData As Variant
  39.     Dim theseRows As String
  40.     For Each rowGroup In folderGroups
  41.         folderData = Split(folderGroups(rowGroup), ",")
  42.         theseRows = folderData(0)
  43.         level = folderData(1)
  44.         With pathRange.rows(theseRows)
  45.             .IndentLevel = level
  46.             If level < MAX_GROUP_LEVEL Then
  47.                 .Group
  48.             End If
  49.         End With
  50.     Next rowGroup
  51. End Sub
  52.  
  53. Private Function SelectFolder() As String
  54.  
  55.     Dim objShell As Object
  56.     Dim objFolder As Object
  57.     Set objShell = CreateObject("Shell.Application")
  58.     Set objFolder = objShell.BrowseForFolder(0, "Select Folder", 0, 17)
  59.     If Not objFolder Is Nothing Then
  60.         SelectFolder = objFolder.self.Path
  61.     End If
  62. End Function
  63.  
  64. Private Function GetAllFiles(ByVal rootPath As String, _
  65.                              Optional onlyFolders As Boolean = False) As Variant
  66.  
  67.     Dim dirOptions As String
  68.     If onlyFolders Then
  69.         dirOptions = """ /a:d-h-s /b /s"
  70.     Else
  71.         dirOptions = """ /a:-h-s /b /s"
  72.     End If
  73.     Dim fOut() As String
  74.     fOut = Split(CreateObject("WScript.Shell").exec("cmd /c dir """ & _
  75.                                                     rootPath & _
  76.                                                     dirOptions).StdOut.ReadAll, _
  77.                  vbNewLine)
  78.     QuickSort fOut, LBound(fOut), UBound(fOut)
  79.  
  80.  
  81.     '    because it's always blank, but add the root folder as the first entry
  82.     Dim pathArray As Variant
  83.     ReDim pathArray(1 To UBound(fOut) + 1, 1 To 1)
  84.     pathArray(1, 1) = rootPath
  85.     Dim i As Long
  86.     For i = 2 To UBound(fOut) + 1
  87.         pathArray(i, 1) = fOut(i - 1)
  88.     Next i
  89.     GetAllFiles = pathArray
  90. End Function
  91.  
  92. Private Function BuildFolderDictionary(ByVal root As String, _
  93.                                        ByRef paths As Variant) As Object
  94.     Dim folders As Object
  95.     Set folders = CreateObject("Scripting.Dictionary")
  96.  
  97.  
  98.     '    noting which items (rows) map into each dictionary
  99.     Dim folder As Variant
  100.     Dim i As Long
  101.     For i = LBound(paths) To UBound(paths)
  102.         Dim pos1 As Long
  103.         If Not IsEmpty(paths(i, 1)) Then
  104.             pos1 = InStrRev(paths(i, 1), "\")   'find the last folder separator
  105.             folder = Left$(paths(i, 1), pos1)
  106.             If Not folders.Exists(folder) Then
  107.                 '--- new (sub)folder, create a new entry
  108.                 folders.Add folder, CStr(i) & ":" & CStr(i)
  109.             Else
  110.                 '--- extisting (sub)folder, add to the row range
  111.                 Dim rows As String
  112.                 rows = folders(folder)
  113.                 rows = Left$(rows, InStr(1, rows, ":"))
  114.                 rows = rows & CStr(i)
  115.                 folders(folder) = rows
  116.             End If
  117.         End If
  118.     Next i
  119.  
  120.  
  121.     '    the entries (runs from the second row to the end)...
  122.     '    and we'll also determine the indent level using the first entry
  123.     '    as the baseline (level 1).  stored as "rows,level" e.g. "2:7,1"
  124.     Dim rootSlashes As Long
  125.     rootSlashes = Len(root) - Len(Replace(root, "\", "")) - 1
  126.     folders(root) = "2:" & UBound(paths) & ",1"
  127.  
  128.     Dim slashes As Long
  129.     folder = folders.Keys
  130.     For i = 1 To UBound(folder)
  131.         slashes = Len(folder(i)) - Len(Replace(folder(i), "\", ""))
  132.         folders(folder(i)) = folders(folder(i)) & "," & _
  133.                                      CStr(slashes - rootSlashes)
  134.     Next i
  135.  
  136.     For Each folder In folders
  137.         Debug.Print folder & " - " & folders(folder)
  138.     Next folder
  139.  
  140.     Set BuildFolderDictionary = folders
  141. End Function
  142.  
  143. Private Sub QuickSort(ByRef Field() As String, ByVal LB As Long, ByVal UB As Long)
  144.  
  145.     Dim P1 As Long, P2 As Long, Ref As String, TEMP As String
  146.  
  147.     P1 = LB
  148.     P2 = UB
  149.     Ref = Field((P1 + P2) / 2)
  150.  
  151.     Do
  152.         Do While (Field(P1) < Ref)
  153.             P1 = P1 + 1
  154.         Loop
  155.  
  156.         Do While (Field(P2) > Ref)
  157.             P2 = P2 - 1
  158.         Loop
  159.  
  160.         If P1 <= P2 Then
  161.             TEMP = Field(P1)
  162.             Field(P1) = Field(P2)
  163.             Field(P2) = TEMP
  164.  
  165.             P1 = P1 + 1
  166.             P2 = P2 - 1
  167.         End If
  168.     Loop Until (P1 > P2)
  169.  
  170.     If LB < P2 Then Call QuickSort(Field, LB, P2)
  171.     If P1 < UB Then Call QuickSort(Field, P1, UB)
  172. End Sub
Tags: excel vba
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement