Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EXCEL - List Search Files

Status
Not open for further replies.

TopJack

Programmer
Mar 10, 2001
153
GB
Can anyone help a beginner here. I want to use VBA in Excel to produce a file list of certain extension types.

I am using the code below but have two problems with it. Firstly if the routine finds a file that is a shortcut to another file, it displays the other file not the source file - How can I display the "real" path and filename (maybe DOS name) ?
Secondly, if a file is missing certain properties, eg File Creation Date, it becomes an "invalid procedure call or argument" - How can I check for validity of property before processing ?

Any help appreciated - thanks in advance.

########################################################

Sub GetFileList()

Dim strDirName As String: strDirName = "c:\"
Dim strFileType As String: strFileType = "*.doc"
Dim Output As Worksheet
Dim oSearch As FileSearch, i As Long, FileScript, _ FileDetails, FileAttribute

Set Output = Worksheets(1)
Set FileScript = CreateObject("Scripting.FileSystemObject")
Set oSearch = Application.FileSearch

With oSearch
.NewSearch
.LookIn = strDirName
.SearchSubFolders = True
.Filename = strFileType

If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
Output.Cells(4, 4).Value = .FoundFiles.Count
For i = 1 To .FoundFiles.Count
Set FileDetails = FileScript.GetFile(.FoundFiles(i))
Select Case FileDetails.Attributes
Case 0: FileAttribute = "Normal"
Case 1: FileAttribute = "Read Only"
Case 2: FileAttribute = "Hidden"
Case 4: FileAttribute = "System"
Case 8: FileAttribute = "Volume"
Case 16: FileAttribute = "Folder"
Case 32: FileAttribute = "Archive"
Case 64: FileAttribute = "Shortcut"
Case 128: FileAttribute = "Compressed"
Case Else: FileAttribute = "Unknown"
End Select
Output.Cells(i + 6, 3).Value = .FoundFiles(i)
Output.Cells(i + 6, 4).Value = FileAttribute
Output.Cells(i + 6, 5).Value = FileDetails.DateCreated
Output.Cells(i + 6, 6).Value = FileDetails.DateLastAccessed
Output.Cells(i + 6, 7).Value = FileDetails.DateLastModified
Next i
Else
MsgBox ("No files found")
End If

End With

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top