This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


List folder contents



dir lists files and folders in the current folder.


dir name lists files and folders that match the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. The name argument can include wildcards (*).


listing = dir(name) returns attributes about name.


collapse all

View Contents of Folder

List the contents of a folder.

dir my_folder

Find Files Matching Specified Name

List all files with a .m extension in the current folder that contain the term my.

dir *my*.m

Find Information in the Return Structure

Return the folder listing of my_folder to the variable MyFolderInfo.

MyFolderInfo = dir('my_folder')

MATLAB® returns the information in a structure array. For example, if my_folder contains three files, the dir command returns this output.

MyFolderInfo = 
3x1 struct array with fields:

Index into the structure to access a particular item.

ans =

Find Date File Last Modified

Get the serial date number for the date and time a file was last modified. Use the datenum field of the structure returned by the dir command.

MyFileInfo = dir('my_file.m');
FileDate = MyFileInfo.datenum

Do not use the datenum function to convert the string returned in the date field of the structure to a number. The results of the datenum function vary depending on the locale. Instead, use the datenum field.

Input Arguments

collapse all

name — Name of file or foldercharacter vector

Name of a file or folder, specified as a character vector. To search for multiple files, use wildcards in the file name. For example, dir *.txt lists all files with a txt extension in the current folder.

The MATLAB dir function is consistent with the Microsoft® Windows® operating system dir command in that both support short file names generated by DOS.

Output Arguments

collapse all

listing — File attributesstructure array

File attributes, returned as a n-by-1 structure array, where n is the number of files and folders returned by the dir command.

This table shows the fields in the structure.

Field Name




File or folder name



Modification date timestamp



Size of the file in bytes



1 if name is a folder; 0 if name is a file



Modification date as serial date number.


More About

collapse all


  • To exclude invalid entries returned by the dir command, use the cellfun function.

    MyFolderInfo = dir; 
    MyFolderInfo = MyFolderInfo(~cellfun('isempty', {})); 

    Invalid entries occur when you run dir with an output argument and the results include a nonexistent file or a file that dir cannot query for some other reason. In this case, dir returns the following default values.

    date: '' 
    bytes: [] 
    isdir: 0 
    datenum: [] 

    Invalid entries most commonly occur on UNIX® platforms when dir queries a symbolic link pointing to a nonexistent target. A nonexistent target is a target that is moved, removed, or renamed.

  • To obtain a list of available drives on Microsoft Windows platforms, use the DOS net use command at the command line.

    dos('net use')

    Or type

    [s,r] = dos('net use')

    MATLAB returns the results to the character array r.

See Also

| | | | | |

Introduced before R2006a

Was this topic helpful?