News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

GetFileList algo demo in SLL form.

Started by hutch--, January 25, 2015, 05:46:51 PM

Previous topic - Next topic

hutch--

I needed an algo of this type for one of my tools where instead of looping through a file list it writes the list to a dynamic string array and returns the file count. The algorithm is clean and easy to use, it requires and empty string array as an argument and it returns the re-dimensioned array filled with file names and the file count. The example attached show how to use it and its no big deal to do.

The algorithm and a support module are built as SLL modules, combined into a library and the library is used in the test piece. The SLL source is supplied. The LIB file is built with BLIB then copied into the parent directory. I build PB files in a non-standard way so the paths will need to be changed. One in MAKEPB.BAT, the other in INCLUDES.INC in the LIB directory.

This is the actual algorithm used in the example.


' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION GetFileList(sarr() as STRING,patn$) COMMON as DWORD

    LOCAL hSearch as DWORD
    LOCAL lcnt as DWORD
    LOCAL rv as DWORD
    LOCAL pfn as STRINGZ PTR
    LOCAL wfd as WIN32_FIND_DATA

    If patn$ = "" Then
      patn$ = ".\*.*"
    End If

  ' ----------------------------------------------------------------
  ' The COUNT loop
  ' ----------------------------------------------------------------
    lcnt = 0

    hSearch = FindFirstFile(ByVal StrPtr(patn$), wfd)
    If hSearch <> %INVALID_HANDLE_VALUE Then
      If wfd.dwFileAttributes <> %FILE_ATTRIBUTE_DIRECTORY Then
        If wfd.nFileSizeLow <> 0 Then
          ! add lcnt, 1
        End If
      End If
    Else
      lcnt = 0
      FUNCTION = lcnt
      Exit FUNCTION
    End If

    Do
      rv = FindNextFile(hSearch,wfd)
      If rv = 0 Then Exit Do
      If wfd.dwFileAttributes <> %FILE_ATTRIBUTE_DIRECTORY Then
        If wfd.nFileSizeLow <> 0 Then
          ! add lcnt, 1
        End If
      End If
    Loop

    FindClose hSearch

    ! sub lcnt, 1

  ' ----------------------------------------------------------------
  ' The ASSIGN loop
  ' ----------------------------------------------------------------
    redim sarr(lcnt) as STRING
    lcnt = 0

    pth$ = path_from_string(patn$)

    hSearch = FindFirstFile(ByVal StrPtr(patn$), wfd)
    If hSearch <> %INVALID_HANDLE_VALUE Then
      If wfd.dwFileAttributes <> %FILE_ATTRIBUTE_DIRECTORY Then
        If wfd.nFileSizeLow <> 0 Then
          pfn = VarPtr(wfd.cFileName)
          sarr(lcnt) = pth$+@pfn
          ! add lcnt, 1
        End If
      End If
    End If

    Do
      rv = FindNextFile(hSearch,wfd)
      If rv = 0 Then Exit Do
      If wfd.dwFileAttributes <> %FILE_ATTRIBUTE_DIRECTORY Then
        If wfd.nFileSizeLow <> 0 Then
          pfn = VarPtr(wfd.cFileName)
          sarr(lcnt) = pth$+@pfn
          ! add lcnt, 1
        End If
      End If
    Loop

    FindClose hSearch

    ! sub lcnt, 1

  ' ----------------------------------------------------------------

    FUNCTION = lcnt

END FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤