home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv4_4 / basic / fcount.bas < prev    next >
Encoding:
BASIC Source File  |  1989-03-23  |  2.1 KB  |  61 lines

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