Extract file List from Explorer to Excel - full path name

JSH720

Board Regular
Joined
Oct 9, 2009
Messages
96
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>
 

JSH720

Board Regular
Joined
Oct 9, 2009
Messages
96
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!
 

Forum statistics

Threads
1,085,334
Messages
5,383,029
Members
401,812
Latest member
topherj09

Some videos you may like

This Week's Hot Topics

Top