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