Advertisement
Nihad

File Path in Excel

Jun 10th, 2025
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ListAllFilesAndFolders()
  2.     Dim ws As Worksheet
  3.     Dim fso As Object
  4.     Dim mainFolder As Object
  5.     Dim subFolder As Object
  6.     Dim file As Object
  7.     Dim folderPath As String
  8.     Dim rowNum As Integer
  9.    
  10.     ' Create or set worksheet
  11.    On Error Resume Next
  12.     Set ws = Sheets("File List")
  13.     On Error GoTo 0
  14.    
  15.     If ws Is Nothing Then
  16.         Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
  17.         ws.Name = "File List"
  18.     End If
  19.    
  20.     ' Clear existing data
  21.    ws.Cells.Clear
  22.     ws.Range("A1:C1").Value = Array("Path", "Name", "Type")
  23.     ws.Range("A1:C1").Font.Bold = True
  24.    
  25.     ' Get folder path from user
  26.    folderPath = InputBox("Enter the folder path:", "Folder Path", "C:\")
  27.     If folderPath = "" Then Exit Sub
  28.    
  29.     ' Check if folder exists
  30.    Set fso = CreateObject("Scripting.FileSystemObject")
  31.     If Not fso.FolderExists(folderPath) Then
  32.         MsgBox "Folder does not exist!", vbExclamation
  33.         Exit Sub
  34.     End If
  35.    
  36.     ' Start listing files and folders
  37.    rowNum = 2
  38.     Application.ScreenUpdating = False
  39.    
  40.     ' Process main folder
  41.    Set mainFolder = fso.GetFolder(folderPath)
  42.    
  43.     ' List all subfolders first
  44.    For Each subFolder In mainFolder.SubFolders
  45.         ws.Cells(rowNum, 1).Value = subFolder.Path
  46.         ws.Cells(rowNum, 2).Value = subFolder.Name
  47.         ws.Cells(rowNum, 3).Value = "Folder"
  48.         rowNum = rowNum + 1
  49.        
  50.         ' Recursively process subfolders
  51.        rowNum = ProcessSubFolder(subFolder, ws, rowNum)
  52.     Next subFolder
  53.    
  54.     ' List all files in main folder
  55.    For Each file In mainFolder.Files
  56.         ws.Cells(rowNum, 1).Value = file.Path
  57.         ws.Cells(rowNum, 2).Value = file.Name
  58.         ws.Cells(rowNum, 3).Value = "File"
  59.         rowNum = rowNum + 1
  60.     Next file
  61.    
  62.     ' Format the worksheet
  63.    ws.Columns("A:C").AutoFit
  64.     ws.Activate
  65.     Application.ScreenUpdating = True
  66.    
  67.     MsgBox "File and folder listing complete!", vbInformation
  68. End Sub
  69.  
  70. Function ProcessSubFolder(parentFolder As Object, ws As Worksheet, startRow As Integer) As Integer
  71.     Dim subFolder As Object
  72.     Dim file As Object
  73.     Dim rowNum As Integer
  74.    
  75.     rowNum = startRow
  76.    
  77.     ' Process all subfolders of the parent folder
  78.    For Each subFolder In parentFolder.SubFolders
  79.         ws.Cells(rowNum, 1).Value = subFolder.Path
  80.         ws.Cells(rowNum, 2).Value = subFolder.Name
  81.         ws.Cells(rowNum, 3).Value = "Folder"
  82.         rowNum = rowNum + 1
  83.        
  84.         ' Recursively process subfolders
  85.        rowNum = ProcessSubFolder(subFolder, ws, rowNum)
  86.     Next subFolder
  87.    
  88.     ' Process all files in the parent folder
  89.    For Each file In parentFolder.Files
  90.         ws.Cells(rowNum, 1).Value = file.Path
  91.         ws.Cells(rowNum, 2).Value = file.Name
  92.         ws.Cells(rowNum, 3).Value = "File"
  93.         rowNum = rowNum + 1
  94.     Next file
  95.    
  96.     ProcessSubFolder = rowNum
  97. End Function
  98.  
Tags: file path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement