mkdir

Make new folder

Description

example

mkdir folderName creates the folder folderName. If folderName exists, MATLAB® issues a warning. If the operation is not successful, mkdir throws an error to the Command Window.

example

mkdir parentFolder folderName creates folderName in parentFolder. If parentFolder does not exist, MATLAB attempts to create it.

example

status = mkdir(___) creates the specified folder and returns a status of 1 if the operation is successful or if the folder exists. Otherwise, mkdir returns 0 and does not throw a warning or error to the Command Window. You can use this syntax with any of the input argument combinations in the previous syntaxes.

example

[status,msg] = mkdir(___) also returns the message text for any warning or error that occurs.

example

[status,msg,msgID] = mkdir(___) additionally returns the message ID for any warning or error that occurs.

Examples

collapse all

Create a folder called newdir in the current folder.

 mkdir newdir

Create a folder called newfolder in the folder testdata. Use a relative path, where newFolder is at the same level as the current folder.

mkdir ../testdata newFolder

Create the same folder twice, verifying the status of the operation after each try.

Create the folder newFolder. The operation succeeds, returning a status of 1 with no error or warning message.

[status, msg, msgID] = mkdir('newFolder')
status = logical
   1

msg =

  0x0 empty char array


msgID =

  0x0 empty char array

Create the folder newFolder again. The operation succeeds again, returning a status of 1. A warning message and message ID inform you that the folder already exists.

[status, msg, msgID] = mkdir('newFolder')
status = logical
   1

msg = 
'Directory already exists.'
msgID = 
'MATLAB:MKDIR:DirectoryExists'

Input Arguments

collapse all

Folder name, specified as a character vector or string scalar. You can specify folderName as an absolute or relative path, unless a parent folder is specified. If you specify a parent folder, then folderName must be a path relative to the parent folder.

If folderName contains a path that includes one or more nonexistent folders, MATLAB attempts to create the nonexistent folder. For example, for the path myFolder\folder1\folder2\targetFolder, if folder1 does not exist, MATLAB creates folder1, creates folder2 within folder1, and creates targetFolder within folder2.

Data Types: char | string

Parent folder for the new folder, specified as a character vector or string scalar. Specify parentFolder as an absolute or relative path. If parentFolder does not exist, MATLAB attempts to create it.

Data Types: char | string

Output Arguments

collapse all

Folder creation status indicating whether the attempt to create the folder is successful, returned as 0 or 1. If the attempt to create the folder is successful or the folder already exists, then the value of status is 1. Otherwise, the value is 0.

Data Types: logical

Error message, returned as a character vector. If an error or warning occurs, msg contains the message text of the error or warning. Otherwise, msg is empty, ''.

Error message identifier, returned as a character vector. If an error or warning occurs, msgID contains the message identifier of the error or warning. Otherwise, msgID is empty, ''.

Introduced before R2006a