How to Quickly List the Contents of a Directory

ID: Q119396


The information in this article applies to:


SUMMARY

Although Visual Basic has both built-in functions (Dir$ and Dir) and a built-in control (File list box) for listing the files in a directory, using a standard list box and the Windows API SendMessage function lists files in a directory much more quickly.

NOTE: Replacing the File list box or Dir command using the technique described in this article will only improve performance perceptibly if the File list box or Dir command is used heavily in the application.


MORE INFORMATION

This technique uses the LB_DIR message for list boxes to add a list of filenames to a list box. To do this, call the Windows API SendMessage function. The SendMessage function requires the following parameters to list the files:


   SendMessage (hWnd%, LB_DIR, wParam%, lParam) 

where

   hWnd%    is the handle of the list box.
   wParam%  is an integer that specifies the permissible file attributes.

   lParam   is a long pointer to a string containing the full path and
            file name to add to the list. This can include wildcards. 

The wParam% argument can be any combination of the following values:

Value             Meaning
------------------------------------------------------------------------
DDL_ARCHIVE       File has been archived.
DDL_DIRECTORY     File must be a directory name.
DDL_EXCLUSIVE     Exclusive flag. If the exclusive flag is set, only
                  files of the specified type are listed. Otherwise,
                  files of the specified type are listed in addition to
                  files that do not match the specified type.
DDL_HIDDEN        File must be hidden.
DDL_READONLY      File must be read only.
DDL_READWRITE     File can be read from or written to.
DDL_SYSTEM        File must be a system file. 

Step-by-Step Example

  1. Start Visual Basic; or, choose New Project from the File menu (ALT+F, N) if Visual Basic is already running. Form1 is created by default.


  2. Add a list box (List1) to Form1.


  3. Add a new code module to the project by choosing New Module from the File menu.


  4. Add the following code to the new code module:
    
       ' Enter the Declare statement on one single line:
       Declare Function SendMessage Lib "user" (ByVal hwnd As Integer,
                                                ByVal wMsg As Integer,
                                                ByVal wParam As Integer,
                                                lParam As Any) As Long
     


  5. Declare the following constants in the General Declaration section of the code module
    
          Global Const WM_USER = &H400
          Global Const LB_DIR = WM_USER + 14
          Global Const DDL_ARCHIVE = &H0020
          Global Const DDL_DIRECTORY = &H0010
          Global Const DDL_EXCLUSIVE = &H8000
          Global Const DDL_HIDDEN = &H0002
          Global Const DDL_READONLY = &H0001
          Global Const DDL_READWRITE = &H0000
          Global Const DDL_SYSTEM = &H0004
     


  6. To fill the list box with all of the files, which may or may not be hidden, in the C:\WINDOWS directory, place the following code in the Form_Load event of Form1:
    
          Sub Form_Load ()
             List1.Clear
             s$ = "C:\WINDOWS\*.*"
             i% = SendMessage(List1.hWnd, LB_DIR, DDL_HIDDEN, ByVal s$)
          End Sub
     


  7. Press the F5 key to run the application.


The contents of the list box can be manipulated by using any of the standard list-box methods. In cases where the filenames are being used elsewhere in the code (for example, in an outline control), the Visible property of the list box can be set to False so that the list box will not be displayed.

Additional query words: speed faster 3.00


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 11, 1999