Documentation

dir

List folder contents

Syntax

Description

dir returns a list of files and folders in the current folder.

example

dir name returns a list of 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. name can include wildcards (*).

example

listing = dir(name) returns attributes about name.

Examples

collapse all

View Contents of Folder

List the contents of a folder.

dir my_dir

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_dir to the variable MyDirInfo.

MyDirInfo = dir('my_dir')

MATLAB® returns the information in a structure array. If my_dir contains three files for example, the dir command returns this output.

MyDirInfo = 
3x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

Index into the structure to access a particular item.

MyDirInfo(3).name
ans =
	my_file.m

Find Date File Was 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 folderstring

Name of file or folder, specified as a string.

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

Description

Class

name

File or folder name

char

date

Modification date timestamp

char

bytes

Size of the file in bytes

double

isdir

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

logical

datenum

Modification date as serial date number.

double

More About

expand all

Tips

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

    MyDirInfo = dir; 
    MyDirInfo = MyDirInfo(~cellfun('isempty', {MyDirInfo.date})); 

    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: [] 
    

    The most common occurrence is on UNIX® platforms when dir queries a file that is a symbolic link, pointing to a nonexistent target. A nonexistent target is a target that was moved, removed, or renamed.

  • To obtain a list of available drives on Microsoft Windows platforms, use the DOS net use command in the Command Window.

    dos('net use')

    Or

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

    MATLAB returns the results to the character array r.

See Also

| | | | | |

Introduced before R2006a

Was this topic helpful?