• DIR function – sort by date (VBA Access 2003)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » DIR function – sort by date (VBA Access 2003)

    Author
    Topic
    #445119

    Using Dir function to return the names of files in a selected folder. Is there a way to specify the order in which the files are returned for subsequent calls to the function? I want the first call to find the newest file, then subsequent calls to find the next newest, etc. until it has traversed the full list of files.

    Actually — finding the newest file would be sufficient, because my program deletes each file after it has been processed. The newest file of the remaining files is what’s necessary.

    Viewing 0 reply threads
    Author
    Replies
    • #1078388

      Dir doesn’t let you specify the order in which files are returned. You can use the Application.FileSearch object for this:

      Dim fs As FileSearch
      Dim i As Integer
      Set fs = Application.FileSearch
      With fs
      .LookIn = “C:My Documents”
      .Filename = “*.*”
      If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 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

      Note: this won’t work any more if/when you upgrade to Access 2007. In its infinite wisdom, Microsoft has decided that searching for files in VBA isn’t necessary any more now that we have desktop search engines, so they removed FileSearch from Office 2007 VBA.

      • #1078415

        Thanks, Hans. Worked like a charm.

      • #1126459

        >> In its infinite wisdom, Microsoft has decided that searching for files in VBA isn’t necessary any more

        Aarrrgh!!! hairout

        • #1126487

          >Aarrrgh!!!
          I gloomily concur.
          Further I think that Hans ought not be allowed to make this the first post I see in my VBA forum digest on a Monday Morning (grin!)
          (Goes back to bed …)

          • #1126500

            Huh? I didn’t do it, no one saw me do it, there’s no way you can prove anything!

            • #1126511

              >Huh? I didn’t do it,
              I believe you.
              Thousands don’t. And won’t.
              Next time you don’t do it, please don’t do it first thing Monday morning.
              Leave it until December 1st when I’ll have something pleasant in my life.

              (On a lighter note) It’s difficult for me to comprehend the logic that allows Dear Old WordBasic to be effective in Word2003, but then decides to make Office 2007 not backward compatible in something related to useful FSO and the like.

              The bottom line: If it hadn’t been for Don Wells’s comment, I wouldn’t have known how miserable I wasn’t.

            • #1126514

              You’ll have something pleasant in your life on December 1st, and you’d like this to be ruined by me not doing something? I can’t guarantee that I won’t try not to do it…

              (I think I need a lie down now)

    Viewing 0 reply threads
    Reply To: DIR function – sort by date (VBA Access 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: