DIRCHANGE() Function

DIRCHANGE() Function Sample

/*
DIRCHANGE()

    Change the current DOS directory

Syntax :

DIRCHANGE( <cDir> ) –> nSuccess

Arguments :

<cDir> is the name of the directory to change to, including the drive.

Returns :

   DIRCHANGE() returns 0 if successful; -1 if there is an argument error. Otherwise, DIRCHANGE() returns the DOS error code.

Description :

  DIRCHANGE() changes the current DOS directory. This function may also be used to determine whether or not a directory exists.

Examples :

  The following example attempts to change to the “c:\dos” directory. If it is unsuccessful, an error message is displayed.

 nResult := DIRCHANGE("c:\dos")
 IF nResult != 0
    ? "Cannot change directory. "
    DO CASE
      CASE nResult == 3
         ?? "Directory does not exist."
      CASE nResult == 5
         ?? "Access to directory denied."
    ENDCASE
    BREAK
 ENDIF

 You may also use something like this:
 DIRCHANGE( "..\..\test" )

 Remarks :

    If <cDir> includs drive specifier, DIRCHANGE() also changes the current drive.

*/

PROC DirChTest()
    LOCAL cDirToChange := "c:\harbour"

    LOCAL cCurDrive := DISKNAME()

    LOCAL nResult

    ? "Current Drive is :", cCurDrive

    nResult := DIRCHANGE( cDirToChange )
    IF nResult = 0
       cCurDrive := DISKNAME()
       ? "Current directory of drive", cCurDrive, "changed to :", CURDIR()
    ELSE
       ? "Cannot change directory to", cDirToChange, ": "
       DO CASE
          CASE nResult = 2
             ?? "File does not exist."
          CASE nResult = 3
             ?? "Directory does not exist."
          CASE nResult = 5
             ?? "Access to directory denied."
          CASE nResult = 15
             ?? "Invalid Drive was specified."
          CASE nResult = 21
             ?? "Drive not ready." 
          OTHE 
             ?? "Unknown error !", nResult
       ENDCASE
    ENDIF

    // Inspect dir existence :
    cDirToChange := "C:\TEMP"
    ? DISKNAME() + ":\" + CURDIR()
    ? cDirToChange, "directory is", IF( IsThisADir( cDirToChange ),;
                                        "", "not" ), "exists." 
    ? DISKNAME() + ":\" + CURDIR()
    ?
    WAIT "End of DIRCHANGE.PRG"

RETU // DirChTest()

/*
 Using DIRCHANGE() function to determine whether or not a directory exists.
*/

FUNC IsThisADir( cDirSpec )
   LOCAL cCurrent := DISKNAME() + "\" + CURDIR(),;
         lRetval := .F.

   IF !EMPTY( cDirSpec )
      lRetval := ( DIRCHANGE( cDirSpec ) = 0 )
      DIRCHANGE( cCurrent )
   ENDIF

RETU lRetval // IsThisADir()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.