Q(uick)BASIC Statement: MKDIR

Quick View

MKDIR

A statement that uses DOS to create a new directory

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
  • MKDIR pathspec
Description/Parameter(s)

The pathname is a string expression specifying the name of the directory to be created. The pathname must be a string of less than 128 characters.

The MKDIR statement works like the DOS command MKDIR; the syntax in BASIC cannot, however, be shortened to MD, as in DOS.

Example

This example creates a subdirectory and creates or copies a file in that directory:

CLS ' Clear screen PRINT "This program creates a subdirectory named MONTHS," PRINT "then creates or copies a file in that directory." PRINT MKDIR "MONTHS" INPUT "Filename"; File$ IF File$ = "" THEN END OPEN "MONTHS" + File$ FOR OUTPUT AS #1 PRINT "You have created "; File$; " in directory MONTHS." CLOSE #1
Syntax
  • MKDIR pathname$
Description/Parameter(s)
pathname$ A string expression that specifies the directory to create in DOS.
  • The argument pathname$ must be a string of fewer than 64 characters.
  • The MKDIR statement works like the DOS command MKDIR. However, the syntax in BASIC cannot be shortened to MD, as it can in DOS.
  • You can use MKDIR to create a DOS directory with a name that contains an embedded space. However, although you can access that directory through DOS, you can remove it only with the BASIC RMDIR statement.
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.

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