GetDir( )

Purpose
Returns a list of subfolders below the specified folder.
 
Author Date of code (original) Updated on Date of page (original) Updated on
Bill Thoen
bthoen@ctmap.com
10Jan2001
10Jan2001
10Jan2001
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 subfolders below the specified folder. The paased parameter, sList() is first redimensioned to 0 elements. If an error occurs in finding the first folder, the user gets the note: "Disk not found." and the function returns with an empty list. The returned list of folders is unsorted.
 
External resources
Uses Windows API functions in Kernel32.
 
Declare statement of sub_function. Include in your program. Copy/Paste if needed.
sub GetDir (
   byval sPath as string, 'Full drive\directory path
   sList() as string)     'Array to receive list of directory 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 GetDir (
   byval sPath as string, 'Full drive\directory path
   sList() as string)     'Array to receive list of directory names

dim hFindFile, nStatus as integer
dim f as WIN32_FIND_DATA
dim i, j as integer
dim sDirPath as string

   redim sList(0)
   sDirPath = sPath
   if right$ (sDirPath, 1) <> "\" then
      sDirPath = sDirPath + "\"
   end if
   
   hFindFile = FindFirstFile (sDirPath+"*.*", f)
   if hFindFile  = INVALID_HANDLE_VALUE then
      nStatus = FindClose (hFindFile)
      note "Disk not found."
      exit sub
   end if
   do  
      if (f.dwFileAttributes \ 16) mod 2 = 1 then
         if f.cFilename <> "." and f.cFilename <> ".." then
            j = j + 1
            redim sList(j)
            sList(j) = f.cFilename
         end if
      end if
      nStatus = FindNextFile (hFindFile, f)
   loop while nStatus = 1
   nStatus = FindClose (hFindFile)
end sub
 
Availability for download
Dir_File.zip
 
Example
'lists all folders below C:\Windows\
dim sFolders() as string
dim i as smallint

   call GetDir("C:\Windows\", sFolders)
   for i = 1 to UBound(sFolders)
      print sFolders(i)
   next
 
Comments

 
See also
GetDrives
GetFilenames