Q(uick)BASIC Statement: CHDRIVE
Quick View
CHDRIVE
Changes the current drive
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- CHDRIVE drive$
Description/Parameter(s)
| drive$ | A string expression that specifies a new default drive. | 
- The argument drive$ must correspond to an existing drive and must be in the range A to lastdrive, where lastdrive is the maximum drive letter you set in your CONFIG.SYS file.
- If you omit the argument drive$, BASIC generates an error.
- If you supply a null argument ("") for drive$, the current drive does not change.
- If the argument drive$ is more than one character, CHDRIVE uses only the first letter.
Usage Notes
- CHDRIVE is not case sensitive. "C" is the same as "c".
Example
This example uses the CURDIR$ function and the CHDRIVE statement within the ChangeDataDrive SUB procedure. CURDIR$ is used to determine the current drive from a given file specification. CHDRIVE changes to a new default drive if the one specified is different from the current drive.
DECLARE SUB ChangeDataDrive (filespec$)
 
ON ERROR RESUME NEXT
 
filespec$ = "A:"
ChangeDataDrive filespec$
 
filespec$ = "C:\DOS"
ChangeDataDrive filespec$
SUB ChangeDataDrive (filespec$)
'This SUB procedure evaluates a file specification and changes the
'currently logged drive to the specified drive (if any). The procedure
'contains no error handling.
 
    curdrive$ = LEFT$(CURDIR$, 2)        'Get the current drive.
 
    'Determine if there is a drive name on the filespec.
    IF INSTR(1, filespec$, ":") = 2 THEN
  newdrive$ = LEFT$(filespec$, 2)
    ELSE
  newdrive$ = curdrive$
    END IF
 
    'Ensure that the new drive is different from the
    'currently logged drive.
    IF newdrive$ <> curdrive$ THEN
  CHDRIVE newdrive$                'It isn't, so change it.
  PRINT "Logged drive changed from "; curdrive$; " to "; newdrive$
    ELSE                                 'It is, so don't change it.
  PRINT "New drive same as logged drive. No change occurred."
    END IF
 
END SUB
              See also: