Extract file List from Explorer to Excel - full path name

JSH720

Board Regular
Joined
Oct 9, 2009
Messages
104
Office Version
  1. 365
Platform
  1. Windows
I found the code below on an earlier post from 2002, but it did not give me the full path, including subfolders and file name. It just gave me the files in that particular folder and did not include the subfolders, etc. How do you change it so that I get the full path? I want to use it for an inventory and as a hyperlink to the file in other documents. I'm pretty new to VBA, so I appreciate your help.
VBA:
Sub ListFiles2()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Dim fileList() As String<o:p></o:p>
Dim fName As String<o:p></o:p>
Dim fPath As String<o:p></o:p>
Dim i As Integer<o:p></o:p>
Dim startrow As Integer<o:p></o:p>
Dim ws As Worksheet<o:p></o:p>
Dim filetype As String<o:p></o:p>
<o:p> </o:p>
'=======================================================<o:p></o:p>
fPath = "C:\Temp\"<o:p></o:p>
filetype = "*"<o:p></o:p>
Set ws = Worksheets("Sheet2")<o:p></o:p>
startrow = 2 'starting row for the data<o:p></o:p>
'========================================================<o:p></o:p>
<o:p> </o:p>
fName = Dir(fPath & "*." & filetype)<o:p></o:p>
While fName <> ""<o:p></o:p>
i = i + 1<o:p></o:p>
ReDim Preserve fileList(1 To i)<o:p></o:p>
fileList(i) = fName<o:p></o:p>
fName = Dir()<o:p></o:p>
Wend<o:p></o:p>
If i = 0 Then<o:p></o:p>
MsgBox "No files found"<o:p></o:p>
Exit Sub<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
For i = 1 To UBound(fileList)<o:p></o:p>
ws.Range("A" & i + startrow).Value = fileList(i)<o:p></o:p>
Next<o:p></o:p>
Columns(1).AutoFit<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
OMG! It works perfectly, it is exactly what I needed, even though it is not exactly what I asked for! 8822 files were indexed within seconds.

THANK YOU!
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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