Announcement

Collapse
No announcement yet.

Getting a fast directory list

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting a fast directory list

    I just discovered that Dir$("*.*", 16) does not filter and
    return only directory entries. For the time being I added a
    filter (GetAttr(FileName$) And 16) = 16, but the code is slow.
    A directory list box gets this data in a fraction of the time.

    What is the right way to parse directories? Also, I'm going to
    want to add a date filter, in case that makes a difference.



    ------------------

  • #2
    As far as BASIC-only methods go, it sounds like you've got it right. In this case,
    you can probably do it faster by making a few Windows API calls. FindFirstFile and
    FindNextFile will not relieve you of the need to filter the results, but they
    return such details as time/date stamps and file attributes as part of a TYPE, so
    it's there at your fingers.

    If you have PB/CC, see your Samples\Util\FileFind.BAS example. The samples can also
    be downloaded from our Downloads area. Or, search the Forums for previous posts on
    this subject.

    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Comment


    • #3
      Following may be of help, see WIN32_FIND_DATA in winapi32.hlp for
      info about file dates.
      Code:
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      ' Populate a Listbox with directories
      ' hWndList is Listbox handle, AbsPath is path to look in, like c:\
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      SUB DirsToList(BYVAL hWndList AS LONG, BYVAL AbsPath AS STRING)
        LOCAL hSearch AS LONG, fRes AS LONG, Path AS STRING, arr() AS STRING, dc AS LONG
        LOCAL WFD AS WIN32_FIND_DATA, I AS LONG
       
        IF RIGHT$(AbsPath, 1) <> "\" THEN AbsPath = AbsPath & "\"
        Path = AbsPath
        AbsPath = AbsPath + "*"
       
        hSearch = FindFirstFile(BYVAL STRPTR(AbsPath), WFD) 'Loop through directory
        IF hSearch <> %INVALID_HANDLE_VALUE THEN
           fRes = %TRUE
           DO WHILE fRes
              IF ASC(WFD.cFileName) <> 46 THEN      'No . or ..
                 IF (WFD.dwFileAttributes AND 16) = 16 THEN 'dirs only, use "<> 16" for files only..
                    AbsPath = TRIM$(WFD.cFileName, CHR$(0))
                    REDIM PRESERVE arr(dc)
                    arr(dc) = Path + AbsPath  '<- remove Path if you only want actual directory names
                    INCR dc
                  END IF
              END IF
              fRes = FindNextFile(hSearch, WFD) ' Get next
           LOOP
           fRes = FindClose(hSearch)
        END IF
       
        ARRAY SORT arr(), COLLATE UCASE '<- sort array, if you like/need to
       
        IF hWndList THEN
           CALL SendMessage(hWndList, %LB_RESETCONTENT, 0, 0) 'clear list
           FOR I = 0 TO UBOUND(arr)
              CALL SendMessage(hWndList, %LB_ADDSTRING, 0, BYVAL STRPTR(arr(I)))
           NEXT
        END IF
       
      END SUB

      ------------------

      Comment


      • #4
        Tom and Borje,

        Thank you for your help. I did not see Borje's reply until
        after I had solved my problem, but I was amazed at how closely
        my final code resembled Borje's code. (My code added the
        wildcard function adapted from pbcc's filefind sample code
        as Tom suggested, otherwise it was the same with only the
        variable names changed.)

        David Lloyd



        ------------------

        Comment

        Working...
        X