Functions and Directives

Function CurDrive() Foundation

Determines or changes the current drive.

Syntax
CurDrive( [<cDrive>] ) --> cOldDrive
Parameters
<cDrive>
<cDrive> is a character expression specifying the drive to select as the current drive.
Return

The return value of CurDrive() is a letter indicating the previously set drive. If no argument is passed to the function, it returns the drive letter of the current drive.

Description

The environment function CurDrive() determines or changes the current drive. The argument <cDrive> can be specified as a character from "a"-"z", "A"-"Z" or with a colon "a:"-"z:", "A:"-"Z:". The corresponding drive becomes the current system drive. When a new drive is selected, the function returns a letter for the drive previously set.

The current directory of a drive is determined or changed with the function CurDir().

Under OS/2, CurDrive() can set a drive which is not ready to run as the current drive without producing a runtime error. A runtime error only occurs when the not ready drive is accessed using a file or environment function.

A runtime error is created under Windows already when the drive specified with <cDrive> is not ready or invalid.

Examples
Read the current drive

// The example demonstrates the results of CurDrive(). 

PROCEDURE Main 
   LOCAL cOldDrive 

   ? CurDrive()                     // result: C 

   cOldDrive := CurDrive("D") 

   ? CurDrive()                     // result: D 
   ? CurDrive(cOldDrive)            // result: D 

   ? CurDrive()                     // result: C 
RETURN 
Determine whether a drive is ready to run

// The example shows the user-defined function 
// IsDriveReady(), which tests the readiness of a drive. 
// When a drive is not ready, a runtime error is generated 
// and is trapped by an error handling code block. 
// IsDriveReady() accounts for the difference of CurDrive() between 
// OS/2 and Windows. 

************** 
PROCEDURE Main                         // test drives for 
   LOCAL cDrive := "C" , nReady        // readiness 

   CLS                                 // clear screen 
   DO WHILE LastKey() <> 27            // terminate with ESC key 

      @ 0,0 SAY "Drive to test:" GET cDrive // enter drive 
      READ 
      nReady := IsDriveReady( cDrive ) // test drive 

      @ 10,10                          // position cursor and 
                                       // clear screen 
      IF nReady == 0                   // display message 
         @ 10,10 SAY  "Drive ready" 
      ELSEIF nReady == -1 
         @ 10,10 SAY  "Drive not ready" 
      ELSE 
         @ 10,10 SAY  "Drive not found or invalid" 
      ENDIF 
   ENDDO 

RETURN 

#define  DRIVE_NOT_READY    21         // OS error code 


******************************* 
FUNCTION IsDriveReady( cDrive )        // Is drive ready ? 
   LOCAL nReturn   := 0 
   LOCAL cOldDrive := CurDrive()       // save current drive 
   LOCAL bError 
   LOCAL oError 

   bError    := ErrorBlock( {|e| Break(e) } ) 
   BEGIN SEQUENCE 

      CurDrive( cDrive )               // change drive 
      CurDir( cDrive )                 // read current directory 

   RECOVER USING oError                // error has occurred 
      ErrorBlock( bError )             // reset error code block 
                                       // for error handling 

      IF oError:osCode == DRIVE_NOT_READY 
         nReturn := -1                 // drive not ready 
      ELSE            
         nReturn := -2                 // drive not available 
      ENDIF                            // or invalid 

   ENDSEQUENCE 

   ErrorBlock( bError )                // reset error code block 

   CurDrive( cOldDrive )               // and drive 

RETURN nReturn 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.