QuickBasic Program to Get Current Drive Using FILES & SCREEN (71525)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1

This article was previously published under Q71525

SUMMARY

The program below uses error trapping, the FILES statement, and the SCREEN function to get the default (current) drive from a Microsoft QuickBasic program.

This information applies to Microsoft QuickBasic versions 4.00, 4.00b, 4.50; and to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS. The program will also work correctly in Microsoft Basic Professional Development System versions 7.00 and 7.10, but Basic PDS contains a CURDIR$ function that returns a string that can be parsed to get the current drive information without using the steps below.

MORE INFORMATION

The program below uses error trapping, the FILES statement, and the SCREEN function to get the current drive. In the program, the FILES statement displays the contents of a directory that does not exist. Because the FILES statement always displays a header of the current directory, we can then trap the error (error 53 -- "File Not Found") that occurs when FILES tries to read the non-existent directory. Then we can use the SCREEN function to read the ASCII character indicating the current drive that FILES displayed on the screen before failing.

This method can be used as an alternative to the CALL INTERRUPT statement for determining the default drive. For more information on CALL INTERRUPT and determining the current drive, query on the following:

CALL and INTERRUPT and default and drive

Code Example:

ON ERROR GOTO Handler   'turn on an error handler
LOCATE 5,1      'use LOCATE statement to position the cursor
                'so you know where the FILES header is displayed.
FILES "\asdf#@jk"   'look for a directory that cannot exist
ON ERROR GOTO 0      'turn off the error handler
LOCATE 10, 5: PRINT CurDrive$; " is the current drive"
END

Handler:
    IF ERR = 53 THEN
    CurDrive$ = CHR$(SCREEN(5, 1))   'use SCREEN function to read
                                     ' ASCII character
    ELSE
    CurDrive$ = "Error"     'If error wasn't #53, then it is a
                            'different error
    END IF
    RESUME NEXT
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB71525