-
Notifications
You must be signed in to change notification settings - Fork 102
/
ListFilesInFolder.vb
104 lines (74 loc) · 2.81 KB
/
ListFilesInFolder.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Sub ListFilesInFolder( _
ByVal SourceFolderName As String, _
Optional ByVal IncludeSubfolders As Boolean)
'Originally created by Leith Ross
'Retreived from http://www.excelforum.com/excel-programming/645683-list-files-in-folder.html
'Lists information about the files in SourceFolder
'Example: ListFilesInFolder "C:\FolderName\", True
On Error GoTo ExitSub
Dim FSO As Object
Dim SourceFolder As Object
Dim SubFolder As Object
Dim FileItem As Object
Dim r As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(SourceFolderName)
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
'display file properties
Cells(r, 1).Formula = FileItem.Name
'***** Remove the ' character in lines below to get information *****
'Cells(r, 2).Formula = FileItem.Path
'Cells(r, 3).Formula = FileItem.Size
'Cells(r, 4).Formula = FileItem.DateCreated
'Cells(r, 5).Formula = FileItem.DateLastModified
'Cells(r, 6).Formula = GetFileOwner(SourceFolder.Path, FileItem.Name)
r = r + 1 ' next row number
'X = SourceFolder.Path
Next FileItem
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
'***** Remove the single ' character in the below lines to adjust the column windths
'Columns("A:G").ColumnWidth = 4
'Columns("H:I").AutoFit
'Columns("J:L").ColumnWidth = 12
'Columns("M:P").ColumnWidth = 8
ExitSub:
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
Function GetFileOwner( _
ByVal FilePath As String, _
ByVal FileName As String)
'Originally created by Leith Ross
'Retreived from http://www.excelforum.com/excel-programming/645683-list-files-in-folder.html
On Error GoTo ExitSub
Dim objFolder As Object
Dim objFolderItem As Object
Dim objShell As Object
FileName = StrConv(FileName, vbUnicode)
FilePath = StrConv(FilePath, vbUnicode)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(StrConv(FilePath, vbFromUnicode))
If Not objFolder Is Nothing Then
Set objFolderItem = objFolder.ParseName(StrConv(FileName, vbFromUnicode))
End If
If Not objFolderItem Is Nothing Then
GetFileOwner = objFolder.GetDetailsOf(objFolderItem, 8)
Else
GetFileOwner = ""
End If
ExitSub:
Set objShell = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
End Function
Sub SampleUsage()
Dim fPath As String
fPath = "V:\Corporate\Tax\Private\Indirect Tax\Certs"
Call ListFilesInFolder(fPath, True)
End Sub