LB Booster
Programming >> BASIC code examples >> Listing a directory in a listbox
http://lbb.conforums.com/index.cgi?board=code&action=display&num=1442826001

Listing a directory in a listbox
Post by Richard Russell on Sep 21st, 2015, 09:00am

Listing the contents of a disk directory in a LISTBOX is trivial, because it's a native function of Windows - although not many people seem to realise that:

Code:
    listbox #w.lb, dummy$(), [click], 10, 10, 280, 300
    open "Directory listing" for window as #w
    hlb = hwnd(#w.lb)
    afsp$ = "C:\Windows\*.*"
    calldll #user32, "SendMessageA", hlb as ulong, _LB_DIR as long, _
        0 as long, afsp$ as ptr, ret as long
    wait 

The API gives you control over what is listed, so for example if you want to include sub-directories as well as files:

Code:
    listbox #w.lb, dummy$(), [click], 10, 10, 280, 300
    stylebits #w.lb _LBS_SORT,0,0,0
    open "Directory listing" for window as #w
    hlb = hwnd(#w.lb)
    afsp$ = "C:\Windows\*.*"
    calldll #user32, "SendMessageA", hlb as ulong, _LB_DIR as long, _
        _DDL_DIRECTORY as long, afsp$ as ptr, ret as long
    wait 

If you prefer the sub-directories at the top, you can do that too:

Code:
    listbox #w.lb, dummy$(), [click], 10, 10, 280, 300
    open "Directory listing" for window as #w
    hlb = hwnd(#w.lb)
    afsp$ = "C:\Windows\*.*"
    calldll #user32, "SendMessageA", hlb as ulong, _LB_DIR as long, _
      _DDL_DIRECTORY or _DDL_EXCLUSIVE as long, afsp$ as ptr, ret as long
    calldll #user32, "SendMessageA", hlb as ulong, _LB_DIR as long, _
      0 as long, afsp$ as ptr, ret as long
    wait 

Richard.

Re: Listing a directory in a listbox
Post by Cor on Nov 21st, 2015, 09:26am

Richard,

Can this code be modified so that its output goes to an array? And can that array be populated such that each subdirectory in it is followed by its contents?

Great code. Just what I needed at this moment. Thank you.
Re: Listing a directory in a listbox
Post by Richard Russell on Nov 21st, 2015, 09:44am

on Nov 21st, 2015, 09:26am, Cor wrote:
Can this code be modified so that its output goes to an array?

Well you could code a loop to read the contents of the listbox into an array, either by using the SELECTINDEX and SELECTION? commands or by using the Windows API (LB_GETTEXT message). But wouldn't it be easier to use the FILES statement and simply arrange to pass the same path and filter parameters as you used for the listbox?

Quote:
And can that array be populated such that each subdirectory in it is followed by its contents?

There are examples of recursive use of the FILES statement to recover the contents of sub-directories. A search of the usual LB code repositories should be fruitful.

Richard.


Re: Listing a directory in a listbox
Post by Cor on Nov 21st, 2015, 1:43pm

Looked for and found a good example of what I want. Thanks.

Cor