GetFileNames( )

Purpose
Retrieves a list of filenames from a folder/directory.
 
Author Date of code (original) Updated on Date of page (original) Updated on
Bill Thoen
bthoen@ctmap.com
15Jan2001
15Jan2001
15Jan2001
29Jan2001
 
Restrictions on use
This module is distributed under the terms of the Lesser GNU General Public License. Restrictions on the use of this work in a commercial application or derivative work is described in the Lesser GNU General Public License page at: http://www.fsf.org/copyleft/lesser.html
 
Description
Returns a list of filenames in the specified folder. The paased parameter, sList() is first redimensioned to 0 elements. If an error occurs in finding the first filename or no filenames of the specified type are found, the function returns with an empty list. The returned list of filenames is unsorted.
 
External resources
Uses Windows API functions in Kernel32
 
Declare statement of sub_function. Include in your program. Copy/Paste if needed.
declare sub GetFileNames (
   byval sPath as string,      'Full directory path
   byval sFilespec as string,  'A file specification (e.g. *.TAB)
   sList() as string)          'Array to receive list of file names
 
Returned value(s) (function only)

 
Other required declare statement(s). Include in your program. Copy/Paste if needed.
'Windows API Definitions, Types and Declarations
define MAX_PATH 260

Type FILETIME
        dwLowDateTime As integer
        dwHighDateTime As integer
End Type
Type WIN32_FIND_DATA
   dwFileAttributes As integer
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As integer
   nFileSizeLow As integer
   dwReserved0 As integer
   dwReserved1 As integer
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type
define INVALID_HANDLE_VALUE -1

Declare Function FindFirstFile Lib "kernel32"
   Alias "FindFirstFileA" (
   ByVal lpFileName As String,
   lpFindFileData As WIN32_FIND_DATA)
   As integer
Declare Function FindNextFile Lib "kernel32"
   Alias "FindNextFileA" (
   ByVal hFindFile As integer,
   lpFindFileData As WIN32_FIND_DATA)
   As integer
Declare Function FindClose Lib "kernel32"
   Alias "FindClose" (
   ByVal hFindFile As Integer)
   As Integer
 
MapBasic Code. Copy/Paste if needed.
sub GetFileNames (
   byval sPath as string,      'Full directory path
   byval sFilespec as string,  'A file specification (e.g. *.TAB)
   sList() as string)          'Array to receive list of file names
dim hFindFile, nStatus as integer
dim f as WIN32_FIND_DATA
dim i as integer

   hFindFile = FindFirstFile (sPath + sFilespec, f)
   if hFindFile <> INVALID_HANDLE_VALUE then
      do  
         if (f.dwFileAttributes \ 16) mod 2 = 0 then
            i = i + 1
            redim sList(i)
            sList(i) = f.cFilename
         end if
         nStatus = FindNextFile (hFindFile, f)
      loop while nStatus = 1
   end if

   nStatus = FindClose (hFindFile)

end sub
 
Availability for download
Dir_File.zip
 
Example
dim sTables() as string
   'Retrieve all table filenames in C:\Data\.
   call GetFileNames ("C:\Data\", "*.tab", sTables)
 
Comments

 
See also
GetDir
GetDrives