Execute Method (FileSearch and FileFind Objects)
Applies To
FileFind object, FileSearch object.
Description
FileSearch object: Begins the search for the specified files.
FileFind object: Begins the search for the specified files and updates the FindFileResults collection. Macintosh only.
Syntax 1
expression.Execute(SortBy, SortOrder, AlwaysAccurate)
Syntax 2
expression.Execute
expression Required. An expression that returns a FileSearch object (Syntax 1) or a FileFind object (Syntax 2).
SortBy Optional Variant. The method used to sort the returned files. Can be one of the following MsoSortBy constants: msoSortbyFileName, msoSortbyFileType, msoSortbyLastModified, or msoSortbySize.
SortOrder Optional Variant. The order in which the returned files are to be sorted. Can be either of the following MsoSortOrder constants: msoSortOrderAscending or msoSortOrderDescending.
AlwaysAccurate Optional Boolean. True to have the file search include files that have been added, modified, or deleted since the file index was last updated. The default value is True.
Example
This example searches for all files that begin with "cmd" in the My Documents folder and displays the location and name of each file that's found. The example sorts the list of returned files in ascending alphabetic order, by file name.
Set fs = Application.FileSearch
With fs
.LookIn = "C:\My Documents"
.FileName = "cmd*"
If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End With