How Basic Can Get Drive Label Using Interrupt or SHELL "DIR" (72275)



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 Q72275

SUMMARY

You can find the drive label for any drive (or floppy disk) from a Microsoft QuickBasic or Microsoft Basic Professional Development System (PDS) program. The following article gives examples of two methods for finding the drive label from a Basic program. The two methods are:

  1. Use Basic's SHELL statement to shell to DOS; then perform a DIR command while redirecting the directory information, which contains the label, into a file. Your program can then open and read this file to determine the drive label. -or-

  2. Use the Basic statement CALL INTERRUPT to call DOS interrupt 21 Hex functions to get a file-control block (FCB). A FCB contains the drive label as part of its information.
This information applies to Microsoft QuickBasic versions 4.00, 4.00b, and 4.50; to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS; and to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS.

MORE INFORMATION

Method 1

To obtain the drive label, SHELL to DOS with the Basic SHELL statement and redirect the DIRectory information into a file. You can then read the file for the label information, which is on the second line of the file after the word "is". The advantage of this method is that the coding is less complex than the code for using CALL INTERRUPT. The disadvantage of this method is that it is slower and uses more memory. Also, when you use the DIR command, if no files are available, a "File not found" message will print to the screen but not to the file. The sample provided below creates a DIR listing of the root drive to lessen the chance that no files will be found.
' SAMPLE PROGRAM to demonstrate how to use SHELL to get the drive
' label.
SHELL "DIR C:\ > temp.tmp"
OPEN "temp.tmp" FOR INPUT AS #1

'  Read the first (blank) line from the file into a dummy variable.
LINE INPUT #1, junk$

' Read the label information
LINE INPUT #1, temp$

' Parse the label from the second line in the file.
label$ = MID$(temp$,INSTR(temp$, "is") + 3)
PRINT "The Label for Drive C: is "; label$
CLOSE #1

'Delete directory file
KILL "temp.tmp"
END
				

Method 2

The second method of getting the directory information is to use Basic's CALL INTERRUPT statement to call DOS functions to get the drive label. The following example uses file-control blocks (FCBs) to get the drive label. Although using FCBs is an outdated method of handling files, FCBs are required to get the drive label information.

More information about DOS interrupts and file-control blocks is available in the Microsoft Press book "Advanced MS-DOS Programming," by Ray Duncan.
DEFINT A-Z
' Start QBX (or QB) with  a /L to load QBX.QLB (or QB.QLB).
' The include file for QB is QB.BI
' The include file for QBX is QBX.BI
REM $INCLUDE: 'QBX.BI'

TYPE XtendedFCB

   ExtendedFCBFlag AS STRING * 1
   Reserved1       AS STRING * 5
   Attribute       AS STRING * 1
   DriveId         AS STRING * 1
   label           AS STRING * 11
   UnUsedFCB       AS STRING * 25
   UnUsedDTA       AS STRING * 84   'DTA should be 128 bytes

END TYPE
DIM inregs AS RegTypeX
DIM outregs AS RegTypeX
DIM fcb AS XtendedFCB
DIM DTA AS XtendedFCB

' set up Variables for change:
DIM Drive AS STRING * 1
Drive = CHR$(0)   'set drive to 0=current, 1 = A, 2 = B, 3 = C, etc.

'save old DTA address:
inregs.ax = &H2F00
CALL InterruptX(&H21, inregs, outregs)
OLDDTASEG = outregs.es
OLDDTAPTR = outregs.bx

'set new DTA address:
inregs.ax = &H1A00
inregs.dx = VARPTR(DTA)
inregs.ds = VARSEG(DTA)
CALL InterruptX(&H21, inregs, outregs)

'Find the first file: the label
fcb.ExtendedFCBFlag = CHR$(&HFF)
fcb.DriveId = Drive

'Do a wildcard search *.* must have the correct spaces:
fcb.label = "*       *"
fcb.Attribute = CHR$(8)
inregs.ax = &H1100
inregs.dx = VARPTR(fcb)
inregs.ds = VARSEG(fcb)
CALL InterruptX(&H21, inregs, outregs)

'  PRINT label:
PRINT DTA.label

'Reset OLD DTA address:
inregs.ax = &H1A00
inregs.dx = OLDDTAPTR
inregs.ds = OLDDTASEG
CALL InterruptX(&H21, inregs, outregs)
				

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