File searching sort order

William
StarLounger
Posts: 80
Joined: 08 Feb 2010, 21:48
Location: Wellington, New Zealand

File searching sort order

Post by William »

While amending some Word 2003 macros to replace the FileSearch object in anticipation of an upgrade to Word 2010, I've noticed that FileSearch and my replacement sort the same files differently. This doesn't seem to have any untoward effect, but I'm curious as to why this is. Can anyone offer an explanation?

For example, if I have the following files in a folder

    absence_from_home_wb3.doc
    age_changes_wb3.doc
    agents_wb3.doc
    annual_review_wb3.doc

FileSearch sorts them in that order (which is also the way that Windows Explorer sorts them, alphabetically ...).

Using my FileSearch replacement, however, the order of the second and third files is reversed - "agents_wb3.doc" now comes before "age_changes_wb3.doc" (it seems that with this option, letters precede underscores).

I use Windows XP.


My FileSearch code is:

Code: Select all

Public Sub FileSearchOption()

  Dim lngEachFileFound As Long

  With Application.FileSearch
    .NewSearch
    .FileName = "*.doc"
    .LookIn = "c:\wb3\"
    .SearchSubFolders = True
    .FileType = msoFileTypeWordDocuments
    .Execute (msoSortByNone)
    For lngEachFileFound = 1 To .FoundFiles.Count
      Documents.Open FileName:=.FoundFiles(lngEachFileFound)
      With ActiveDocument
        MsgBox .Name
        .Close SaveChanges:=wdDoNotSaveChanges
      End With
    Next lngEachFileFound
  End With

End Sub
(Replacing ".Execute (msoSortByNone)" in the code above with ".Execute (msoSortByFileName)" doesn't make any difference.)


My replacement code is:

Code: Select all

Public Sub DirOption()

  DirectoryFileSearch "c:\wb3\"

End Sub

Private Sub DirectoryFileSearch(Path As String)

  Dim strDirectory As String
  Dim strDirectoryList() As String
  Dim intArraySize As Integer
  Dim intPosition As Integer

  strDirectory = Dir(Path, vbDirectory)

  Do While strDirectory <> ""
    If strDirectory <> "." And strDirectory <> ".." Then
      If (GetAttr(Path & strDirectory) And vbDirectory) = vbDirectory Then
        If (Not strDirectoryList) = True Then
          ReDim strDirectoryList(0 To 0)
        Else
          ReDim Preserve strDirectoryList(0 To UBound(strDirectoryList) + 1)
        End If
        strDirectoryList(UBound(strDirectoryList)) = strDirectory
      Else
        intPosition = InStrRev(strDirectory, ".")
        If intPosition > 0 Then
          If InStr(1, "doc", LCase(Right(strDirectory, Len(strDirectory) - intPosition))) Then
            Documents.Open FileName:=Path & strDirectory
            With ActiveDocument
              MsgBox .Name
              .Close SaveChanges:=wdDoNotSaveChanges
            End With
          End If
        End If
      End If
    End If
    strDirectory = Dir
  Loop

  If (Not strDirectoryList) = True Then
  Else
    For intArraySize = 0 To UBound(strDirectoryList)
      DirectoryFileSearch Path & strDirectoryList(intArraySize) & Application.PathSeparator
    Next
  End If
    
End Sub

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: File searching sort order

Post by HansV »

In most situations, the exact order of the files is not important. If it is, you could store the file names in an array and sort that array.
Best wishes,
Hans