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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application.FileSearch wrong result 1

Status
Not open for further replies.

JerryKlmns

IS-IT--Management
Feb 7, 2005
2,062
GR
I have a function retrieving the latest file on a w2000 server folder

Code:
Function FileSearching(ByVal File_Name As String, ByVal Search_Folder As String) As String

With Application.FileSearch
    .NewSearch
    .FileType = 1 'msoFileTypeAllFiles
    .Filename = File_Name & "*.txt"
    .LookIn = Search_Folder
    .SearchSubFolders = False
    If .Execute(4, 2, True) > 0 Then 
'msoSortByLastModified, msoSortOrderDescending
        FileSearching = .FoundFiles(.FoundFiles.Count)
    Else
        MsgBox "No File Found", vbCritical + vbOKOnly, "Error"
        FileSearching = "NoFile"
    End If
End With

End Function

When run it should return PROMLET041105.TXT (Modified 04-Nov-2005) and not PROMLET051005.TXT (Modified 05-Oct-2005)

The DateLastModified property using fso for PROMLET051005 is 05/10/2005 07:51:21 and for PROMLET041105 is 04/11/2005 09:23:48.

Is this normal ?
This function works fine for 3 other files.
 
Hi Jerry,

Looking at this I believe that you had the two numbers in the .Execute method down incorrectly.

As far as I can tell your search would read like this:
Code:
If .Execute(msoSortByNone, msoSortDescending, True) > 0 Then
Obviously this isn't what you are after.
Then I tried this
Code:
If .Execute(3, 1, True) > 0 Then
This seemed to return the files in the correct date order (i.e. the latest)
The final code I used was using the constant names as this is easier to read and understand
Code:
If .Execute(msoSortByLastModified, msoSortAscending, True) > 0 Then
I have done it like this because if you are using the
Code:
.FoundFiles(.FoundFiles.Count)
to determine the last file, sorting it descendingly would get you the earliest file as far as I can tell. If you use an ascending sort the last file will be the latest as you require.

Sorry that was a bit long-winded but I hope it can help

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Thanx HarleyQuinn

This is now the correct function

Code:
Function FileSearching(ByVal File_Name As String, ByVal Search_Folder As String) As String

With Application.FileSearch
    .NewSearch
    .FileType = msoFileTypeAllFiles
    .Filename = File_Name & "*.txt"
    .LookIn = Search_Folder
    .SearchSubFolders = False
    If .Execute(msoSortByLastModified, msoSortOrderAscending, True) > 0  Then 
        FileSearching = .FoundFiles(.FoundFiles.Count)
    Else
        MsgBox "No File Found", vbCritical + vbOKOnly, "Error"
        FileSearching = "NoFile"
    End If
End With

 
You're welcome Jerry, glad I could help [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top