home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 101.img / QB45-1.ZIP / FILERR.BAS < prev    next >
BASIC Source File  |  1987-09-23  |  3KB  |  106 lines

  1. ' Declare symbolic constants used in program:
  2. CONST FALSE = 0, TRUE = NOT FALSE
  3.  
  4. DECLARE FUNCTION GetFileName$ ()
  5.  
  6. ' Set up the ERROR trap, and specify the name of the
  7. ' error-handling routine:
  8. ON ERROR GOTO ErrorProc
  9.  
  10. DO
  11.    Restart = FALSE
  12.    CLS
  13.  
  14.    FileName$ = GetFileName$     ' Input file name.
  15.  
  16.    IF FileName$ = "" THEN
  17.       END                       ' End if <ENTER> pressed.
  18.    ELSE
  19.  
  20.       ' Otherwise, open the file, assigning it the
  21.       ' next available file number:
  22.       FileNum = FREEFILE
  23.       OPEN FileName$ FOR INPUT AS FileNum
  24.    END IF
  25.  
  26.    IF NOT Restart THEN
  27.  
  28.       ' Input search string:
  29.       LINE INPUT "Enter string to locate: ", LocString$
  30.       LocString$ = UCASE$(LocString$)
  31.  
  32.       ' Loop through the lines in the file, printing them
  33.       ' if they contain the search string:
  34.       LineNum = 1
  35.       DO WHILE NOT EOF(FileNum)
  36.  
  37.          ' Input line from file:
  38.          LINE INPUT #FileNum, LineBuffer$
  39.  
  40.          ' Check for string, printing the line and its
  41.          ' number if found:
  42.          IF INSTR(UCASE$(LineBuffer$), LocString$) <> 0 THEN
  43.             PRINT USING "#### &"; LineNum, LineBuffer$
  44.          END IF
  45.  
  46.          LineNum = LineNum + 1
  47.       LOOP
  48.  
  49.       CLOSE FileNum             ' Close the file.
  50.  
  51.    END IF
  52. LOOP WHILE Restart = TRUE
  53.  
  54. END
  55.  
  56. ErrorProc:
  57.  
  58.    SELECT CASE ERR
  59.  
  60.       CASE 64:                  ' Bad File Name
  61.          PRINT "** ERROR - Invalid file name"
  62.  
  63.          ' Get a new file name and try again:
  64.          FileName$ = GetFileName$
  65.  
  66.          ' Resume at the statement that caused the error:
  67.          RESUME
  68.  
  69.       CASE 71:                  ' Disk not ready
  70.          PRINT "** ERROR - Disk drive not ready"
  71.          PRINT "Press C to continue, R to restart, Q to quit: "
  72.          DO
  73.             Char$ = UCASE$(INPUT$(1))
  74.             IF Char$ = "C" THEN
  75.                RESUME           ' Resume where you left off
  76.  
  77.             ELSEIF Char$ = "R" THEN
  78.                Restart = TRUE   ' Resume at beginning
  79.                RESUME NEXT
  80.  
  81.             ELSEIF Char$ = "Q" THEN
  82.                END              ' Don't resume at all
  83.             END IF
  84.          LOOP
  85.  
  86.       CASE 53, 76:              ' File or path not found
  87.          PRINT "** ERROR - File or path not found"
  88.          FileName$ = GetFileName$
  89.          RESUME
  90.  
  91.       CASE ELSE:                ' Unforeseen error
  92.  
  93.          ' Disable error trapping and print standard
  94.          ' system message:
  95.          ON ERROR GOTO 0
  96.    END SELECT
  97. '
  98. ' ======================= GETFILENAME$ =======================
  99. '              Returns a file name from user input
  100. ' ============================================================
  101. '
  102. FUNCTION GetFileName$ STATIC
  103.    INPUT "Enter file to search (press ENTER to quit): ", FTemp$
  104.    GetFileName$ = FTemp$
  105. END FUNCTION
  106.