Create file list for a directory / folder

hocs

New Member
Joined
Nov 13, 2009
Messages
33
I got the following codes from others.
Would appreciate if someone could improve the following codes:

1. To have a column (to the left of column 'Name'), which shows the folder path and with hyperlink. i.e. if the file is found at G drive folder called 'Sound', it will show G:\Sound. when i click on the hyperlink, it will open this folder.

2. To insert hyperlink for each individual files under column 'Name', so that when hyperlink is clicked, it will open the file.

Thanks!
-----------------------------------------------------------------------

Option Explicit
'API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) _
As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Sub FileInfo()
Dim c As Long, r As Long, i As Long
Dim FileName As Object 'FolderItem2
Dim objShell As Object 'IShellDispatch4
Dim objFolder As Object 'Folder3

' Create the object
Set objShell = CreateObject("Shell.Application")

' Prompt for the folder
Set objFolder = objShell.Namespace(GetDirectory)
' Insert headers on active sheet
Worksheets.Add
c = 0
For i = 0 To 34
If i = 27 Or i = 28 Or i = 29 Or i = 31 Then
'Nothing. These items are not used
Else
c = c + 1
Cells(1, c) = objFolder.GetDetailsOf(objFolder.Items, i)
End If
Next i
' Loop through the files
r = 1
For Each FileName In objFolder.Items
c = 0
r = r + 1
For i = 0 To 34
If i = 27 Or i = 28 Or i = 29 Or i = 31 Then
'Nothing. These items are not used
Else
c = c + 1
Cells(r, c) = objFolder.GetDetailsOf(FileName, i)
End If
Next i
Next FileName
' Make it a table
ActiveSheet.ListObjects.Add xlSrcRange, _
Range("A1").CurrentRegion
End Sub
Function GetDirectory(Optional Msg) As String
' Returns the directory specified by the user
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer

' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If

' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
x = SHBrowseForFolder(bInfo)

' Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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