home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12334.ZIP / READNAME.BAS < prev    next >
BASIC Source File  |  1989-03-23  |  2KB  |  58 lines

  1. '***** ReadName.Bas - contains BASIC function returning an array of file names
  2.  
  3. 'syntax: Count = FileCount%(FileSpec$)
  4. '        DIM Array$(Count)
  5. '        CALL ReadNames(FileSpec$, Array$())
  6. ' where: FileSpec$ is in the form "*.BAS" or "A:\subdir\*.*", and so 
  7. '        forth, and Array$() is filled in with each file's name
  8.  
  9. DEFINT A-Z
  10.  
  11. TYPE FileFindBuf
  12.     MiscInfo AS STRING * 22        'Receives date, time, size, etc.
  13.     FileLen  AS STRING * 1        'Receives the length of the name
  14.     FileName AS STRING * 13        'Receives the name
  15. END TYPE
  16.  
  17.  
  18. DECLARE FUNCTION _
  19.     DosFindFirst%(BYVAL SpecSeg, BYVAL SpecAdr, _
  20.               SEG Handle, BYVAL Attrib, SEG Buffer _
  21.               AS FileFindBuf, BYVAL BufLen, SEG MaxCount, _
  22.               BYVAL Reserved&)
  23.  
  24. DECLARE FUNCTION DosFindNext%(BYVAL Handle, SEG Buffer AS FileFindBuf, _
  25.                   BYVAL BufLen, SEG MaxCount)
  26.  
  27. DECLARE SUB DosFindClose(BYVAL Handle)
  28.  
  29. SUB ReadNames(FileSpec$, Array$())
  30.  
  31.    FSpec$ = FileSpec$ + CHR$(0) 'create an ASCIIZ string from 
  32.                                 'the file spec
  33.    Handle = -1            'request a new search handle
  34.    Attrib = 39                  'matches all file types except 
  35.                                 'subdirectory
  36.    DIM Buffer AS FileFindBuf    'create a buffer to hold the 
  37.                                 'returned info
  38.    BufLen = 36                  'tells OS/2 how big the buffer is
  39.    MaxCount = 1                 'get just one file
  40.    Reserved& = 0                'this is needed, but not used
  41.  
  42.                                 'call DosFindFirst
  43.    ErrorCode = DosFindFirst%(VARSEG(FSpec$), SADD(FSpec$), Handle, _
  44.                Attrib, Buffer, BufLen, MaxCount, Reserved&)
  45.  
  46.    IF ErrorCode THEN EXIT SUB    'no matching files, exit
  47.    Count = 0                     'initialize counter
  48.  
  49.    DO
  50.       Count = Count + 1               'we got another one
  51.       Length = ASC(Buffer.FileLen)    'get the name's length
  52.       Array$(Count) = LEFT$(Buffer.FileName, Length) 'assign the name
  53.    LOOP WHILE (DosFindNext%(Handle, Buffer, BufLen, MaxCount) = 0)
  54.  
  55.    DosFindClose(Handle)               'close the handle
  56.  
  57. END SUB
  58.