home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / BASIC / POWBASIC / LIBRARY1 / FNDFIL.ZIP / FINDFILE.BAS < prev    next >
BASIC Source File  |  1990-02-19  |  5KB  |  136 lines

  1.  ' SEE THE COMMENT BEFORE THE SAMPLE ABOUT COMPILING THIS
  2.  ' Copr 1988, 1989 Barry Erick
  3.  $COMPILE UNIT
  4.  $LIB ALL OFF
  5.  $ERROR ALL OFF
  6.  
  7.  FUNCTION Findfirst$(filn$)  public
  8.  'Copr 1988,1989 Barry Erick
  9. LOCAL stringsegptr&,ferr%
  10. LOCAL DTAAtt&,DTAOff&,DTASeg&,FExist$,Fil,Fil$,Ptr
  11.  
  12.  '  Format of the DTA after a file has been found:
  13.  '
  14.  '      Offset      Size(bytes)    Description
  15.  '     (D) (H)
  16.  '       0  0       21  15       Used by DOS for find next
  17.  '      21  15       1   1       Attribute of file found
  18.  '      22  16       2   2       Time Stamp of file
  19.  '      24  18       2   2       Date Stamp of file
  20.  '      26  1A       4   4       File size in bytes
  21.  '      30  1E      13   D       Filename and extension (asciiz)
  22.  '
  23.  '  Attributes:
  24.  '     bit    0 - READ Only
  25.  '            1 - Hidden
  26.  '            2 - SYSTEM
  27.  '            3 - Volume Label
  28.  '            4 - Subdirectory
  29.  '            5 - Archive
  30.  
  31.  ' The formats for Time and Date are:
  32.  '  Time = Hour * 2048 + Minute * 32 + Second / 2)
  33.  '  Date = (Year - 1980) * 512 + Month * 32 + Day)
  34.  '
  35.  ' see the file Exists.Bas for the routines to turn the time and date into
  36.  ' their members
  37.  
  38.  fexist$          = filn$+CHR$(0)       ' make it a ASCIIZ string for DOS
  39.  REG 8,strseg(fexist$)                  ' String Segment  to Reg DS
  40.  REG 4,strptr(Fexist$)                  ' String Seg Offset to Reg DX
  41.  REG 3,&H17                             ' Find all but vol label Attribute
  42.  REG 1,&H4E00                           ' DOS Function Find First Match
  43.  CALL INTERRUPT &H21                    ' Just look for first matching file
  44.  ferr% = REG(1)                         ' Reg AX.. 0 = no error
  45.  IF ferr% = 2 OR_
  46.     ferr% = 18 OR_
  47.     ferr% = 3 OR_
  48.     ferr% = 15 THEN
  49.     Findfirst$ = ""
  50.     EXIT Function
  51.  END IF
  52.  ' Get the dta that has the filename & stuff
  53.  REG 1,&H2F00                               ' ah = 2F (Get DTA)
  54.  CALL INTERRUPT &h21                        ' perform dos call
  55.  dtaseg& = REG(9)                           ' DTA segment = ES
  56.  dtaatt& = REG(2)+&H15                      ' offset of attributes
  57.  dtaoff& = REG(2)+&H1E                      ' offset of filename
  58.  fil$ = ""                                  ' prepare to retrieve filename
  59.  DEF SEG=dtaseg&                            ' set segment = DTA segment
  60.  FOR ptr% = 0 TO 12                         ' retrieve filename
  61.      fil% = PEEK(dtaoff& + ptr%)                  ' from DTA
  62.      IF fil% = 0 THEN EXIT FOR
  63.      fil$ = fil$ + CHR$(PEEK(dtaoff& + ptr%))
  64.  NEXT ptr%
  65.  IF (PEEK(dtaatt&) AND 16) = 16 THEN
  66.     fil$ = "<"+fil$+">"' its a subdir ' look late to see if we really want it
  67.  END IF
  68.  DEF SEG                                    ' reset segment to default
  69.  Findfirst$ = fil$
  70.  END Function
  71.  
  72.  '[********************]
  73.  
  74.  FUNCTION Findnext$                public
  75.  'Copr 1988,1989 Barry Erick
  76. LOCAL fil$,dtaseg&,dtaoff&,dtaatt&,fil%,ptr%
  77.  
  78.  REG 1,&h4F00
  79.  CALL INTERRUPT &h21
  80.  ' return if errors and/or no files
  81.  IF REG(1) = 18 THEN
  82.     Findnext$ = ""
  83.     EXIT Function
  84.  END IF
  85.  REG 1,&H2F00
  86.  CALL INTERRUPT &H21
  87.  dtaseg& = REG(9)   ' DTA segment = ES
  88.  dtaatt& = REG(2) + 21' offset of attributes
  89.  dtaoff& = REG(2) + 30' filename offset
  90.  fil$ = ""          ' prepare to transfer filename
  91.  DEF SEG=dtaseg&    ' set segment to DTA segment
  92.  FOR ptr% = 0 TO 12
  93.      fil% = PEEK(dtaoff& + ptr%)                    ' from DTA
  94.      IF fil% = 0 THEN EXIT FOR
  95.      fil$ = fil$ + CHR$(PEEK(dtaoff& + ptr%))
  96.  NEXT ptr%
  97.  IF (PEEK(dtaatt&) AND 16) = 16 THEN
  98.     fil$ = "<"+fil$+">"' mark subdirs, mask later if need be
  99.  END IF
  100.  DEF SEG                                    ' restore default segment
  101.  Findnext$ = fil$
  102.  END FUNCTION
  103.  
  104.  '[********************]
  105.  
  106.  '**********************************************************************
  107.  ' The following is a sample of how to use this                        *
  108.  ' test findfile                                                       *
  109.  ' This sample will not compile unless you change %NotComment to = -1  *
  110.  '**********************************************************************
  111.  
  112.  %notcomment = 0
  113.  $IF  %notcomment
  114.  
  115.      INPUT "mask";mask$       'mask can be wildcards
  116.      numfound% = 0
  117.      a$=Findfirst$(mask$)
  118.      IF a$="" THEN
  119.         PRINT mask$ ;"Not found"
  120.         END
  121.      ELSE
  122.         PRINT mask$;" found!"
  123.         INCR numfound%
  124.         PRINT USING "\              \";a$; 'print the first file
  125.         DO
  126.             a$=Findnext$
  127.             IF a$="" THEN EXIT LOOP
  128.             PRINT USING "\               \";a$;'print the rest
  129.             INCR numfound%                  'note, you can save names
  130.         LOOP                                'by sticking them in an
  131.         PRINT                               'array, but for the demo, we
  132.         PRINT numfound%-1 ;"Files found"    'just print them
  133.      END IF
  134.  
  135.  $ENDIF
  136.