Q(uick)BASIC Statement: CHDIR

Quick View

CHDIR

A statement that uses DOS to change the current default directory for the specified drive

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • CHDIR pathname$
  • MKDIR pathname$
  • RMDIR pathname$
  • FILES [filespec$]
Description/Parameter(s)

CHDIR changes a drive's default directory. MKDIR creates a subdirectory. RMDIR removes a subdirectory. FILES displays the contents of the current directory or a specified directory.

pathname$ The path of the new default directory, subdirectory to create, or subdirectory to remove.
filespec$ A filename or path (may include a drive and DOS wildcard characters). If you don't specify a filespec$, FILES displays all files in the current directory.
Example
MKDIR "C:\TEMP\TEST" CHDIR "C:\TEMP" FILES RMDIR "TEST"
Syntax
  • CHDIR pathspec
Description/Parameter(s)
pathspec A string expression that identifies the directory that is to become the default directory

The pathspec must have fewer than 64 characters. It has the following syntax:

  • [drive:][\]directory[\directory]…

The argument drive: is an optional drive specification. If you omit drive, CHDIR changes the default directory on the current drive.

CHDIR differs from the CHDIR command in DOS in two ways:

  1. The BASIC statement cannot be shortened to CD.
  2. There is no form of the CHDIR statement that returns the current directory.
Note: The CHDIR statement changes the default directory but not the default drive. For example, if the default drive is C, then the following CHDIR statement changes the default directory on drive D, but the default drive is still C:
  • CHDIR "D:\TMP"
Examples
' Makes HOMESALES the current directory on the default ' drive. CHDIR "HOMESALES"
' Changes the current directory to USERS on drive B; it does ' not, however, change the default drive to B. CHDIR "B:USERS"
Syntax
  • CHDIR pathname$
Description/Parameter(s)
  • The argument pathname$ must have fewer than 64 characters. It has the following syntax:
    [drive:][\]]directory[\directory]…
  • The argument drive: is an optional drive specification. If you omit drive, CHDIR changes the default directory on the current drive.

Usage Notes

  • CHDIR cannot be shortened to CD.
  • CHDIR changes the default directory but not the default drive. For example, if the default drive is C, the following statement changes the default directory on drive D, but C remains the default drive:

  • CHDIR "D:\TMP"

  • Use CURDIR$ to return the current directory and CHDRIVE to change the default drive.
Example

This example uses the MKDIR statement to create a subdirectory. The AddADir procedure uses the CHDIR statement with the ON ERROR statement to check if the subdirectory already exists. If it does not exist, the error-handling routine traps error 76, prompts the user, and creates the directory.

DECLARE SUB AddADir (DestDir$) CLS AddADir "TESTDIR" SUB AddADir (DestDir$) ON LOCAL ERROR GOTO ErrHandler 'Set up the local error handler. CurDirName$ = CURDIR$ 'Preserve the name of the current directory. CHDIR DestDir$ 'Change to DestDir$ - Error 76 if not exist. CHDIR CurDirName$ 'Change back to original directory. EXIT SUB ErrHandler: IF ERR = 76 THEN PRINT "Directory does not exist. Create ?"; Ans$ = INPUT$(1) PRINT Ans$ IF UCASE$(Ans$) = "Y" THEN MKDIR DestDir$ 'Make the directory. PRINT DestDir$; " directory created." RESUME NEXT ELSE PRINT DestDir$; " directory NOT created." END IF 'Return control to caller. END IF RESUME NEXT 'Just continue. END SUB