PDA

View Full Version : [SOLVED:] Solved: List Files in Directory that Match a File Extension



RandiJ
03-09-2011, 08:49 PM
Can the following code be modified so that it lists only files of a certain extension as opposed to excluding extensions? Ex: Just want to pull Visio files (".vsd").

Option Compare Text
Option Explicit

Function Excludes(Ext As String) As Boolean
'Function purpose: To exclude listed file extensions from hyperlink listing

Dim X, NumPos As Long

'Enter/adjust file extensions to EXCLUDE from listing here:
X = Array("exe", "bat", "dll", "zip", "vsd")

On Error Resume Next
NumPos = Application.WorksheetFunction.Match(Ext, X, 0)
If NumPos > 0 Then Excludes = True
On Error GoTo 0

End Function

Sub HyperlinkFileList()
'Macro purpose: To create a hyperlinked list of all files in a user
'specified directory, including file size and date last modified
'NOTE: The 'TextToDisplay' property (of the Hyperlink object) was added
'in Excel 2000. This code tests the Excel version and does not use the
'Texttodisplay property if using XL 97.

Dim fso As Object, _
ShellApp As Object, _
SubFolder As Object, _
Directory As String, _
Problem As Boolean, _
ExcelVer As Integer

'Turn off screen flashing
Application.ScreenUpdating = False

'Create objects to get a listing of all files in the directory
Set fso = CreateObject("Scripting.FileSystemObject")

'Prompt user to select a directory
Do
Problem = False
Set ShellApp = CreateObject("Shell.Application"). _
Browseforfolder(0, "Please choose a folder", 0, "c:\\")

On Error Resume Next
'Evaluate if directory is valid
Directory = ShellApp.self.Path
Set SubFolder = fso.GetFolder(Directory)
If Err.Number <> 0 Then
If MsgBox("You did not choose a valid directory!" & vbCrLf & _
"Would you like to try again?", vbYesNoCancel, _
"Directory Required") <> vbYes Then Exit Sub
Problem = True
End If
On Error GoTo 0
Loop Until Problem = False

'Set up the headers on the worksheet
With ActiveSheet
With .Range("A1")
.Value = "Listing of all files in:"
.ColumnWidth = 40
'If Excel 2000 or greater, add hyperlink with file name
'displayed. If earlier, add hyperlink with full path displayed
If Val(Application.Version) > 8 Then 'Using XL2000+
.Parent.Hyperlinks.Add _
Anchor:=.Offset(0, 1), _
Address:=Directory
Else 'Using XL97
.Parent.Hyperlinks.Add _
Anchor:=.Offset(0, 1), _
Address:=Directory
End If
End With
With .Range("A2")
.Value = "File Name"
.Interior.ColorIndex = 15
With .Offset(0, 1)
.ColumnWidth = 15
.Value = "Date Created"
.Interior.ColorIndex = 15
.HorizontalAlignment = xlCenter
End With
With .Offset(0, 2)
.ColumnWidth = 15
.Value = "Date Last Modified)"
.Interior.ColorIndex = 15
.HorizontalAlignment = xlCenter
End With
End With
End With

Call GetFiles(SubFolder)
End Sub

Private Sub GetFiles(ByRef Folder As Object)
'Adds each file, details and hyperlinks to the list
Dim File As Object
Dim SubFolder As Object

For Each File In Folder.Files
If Not Excludes(Right(File.Path, 3)) = True Then
With ActiveSheet
'If Excel 2000 or greater, add hyperlink with file name
'displayed. If earlier, add hyperlink with full path displayed
If Val(Application.Version) > 8 Then 'Using XL2000+
.Hyperlinks.Add _
Anchor:=ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0), _
Address:=File.Path
Else 'Using XL97
.Hyperlinks.Add _
Anchor:=ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0), _
Address:=File.Path
End If
'Add date last modified, and date created
With .Range("A65536").End(xlUp)
.Offset(0, 1) = File.DateCreated
.Offset(0, 2) = File.DateLastModified
End With
End With
End If
Next

For Each SubFolder In Folder.subfolders
With ActiveSheet.Range("A65536").End(xlUp).Offset(1, 0)
.Value = "SubFolder"
.Offset(0, 1).Value = SubFolder.Path
End With

Call GetFiles(SubFolder)
Next SubFolder

End Sub
Thank you.

Kenneth Hobs
03-09-2011, 10:09 PM
Change:
If Not Excludes(Right(File.Path, 3)) = True Then

to:
If UCase(Right(File.Path, 3)) = "VSD" Then

RandiJ
03-10-2011, 11:20 AM
Thank you Kenneth! Worked like a charm.:2jump: