dir

List folder contents

Syntax

dir
dir name
listing = dir(name)

Description

dir lists the files and folders in the MATLAB® current folder. Results appear in the order returned by the operating system.

dir name lists the 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. You can use wildcards (*).

listing = dir(name) returns attributes about name.

Input Arguments

name

A string value specifying a file or folder name.

Output Arguments

listing

Field Name

Description

Class

name

File or folder name

char array

date

Modification date timestamp

char array

bytes

Size of the file in bytes

double

isdir

1 if name is a folder; 0 if not

logical

datenum

Modification date as serial date number. The value is locale-dependent.

double

Examples

View the Contents of a Folder

View the contents of the matlab/audiovideo folder:

dir(fullfile(matlabroot, 'toolbox/matlab/audiovideo'))

Find Information in the Return Structure

Return the folder listing, restricted to files with a .m extension, to the variable av_files:

av_files = dir(fullfile(matlabroot, ...
               'toolbox/matlab/audiovideo/*.m'))

MATLAB returns the information in a structure array:

av_files = 
25x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

Index into the structure to access a particular item:

av_files(3).name
ans =
   audioplayerreg.m

– Use the Wildcard Character to Find Multiple Files

View the MAT-files in the current folder that include the term my_data by using the wildcard character:

dir *my_data*.mat

MATLAB returns all file names that match this specification. For instance, it returns the following if they are in the current folder:

old_my_data.mat  my_data_final.mat  my_data_test.mat

– Exclude Certain Files from the Output

Return the list of files in the current folder, excluding those files that dir cannot query:

 y = dir; 
 y = y(find(~cellfun(@isempty,{y(:).date}))); 

– Find the Date a File Was Modified

To 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:

DirInfo = dir('startup.m');
filedate = DirInfo.datenum

Using the datenum function to convert the string returned in the date field of the structure is not recommended as this can behave differently in different locales.

filedate = datenum(DirInfo.date)

Alternatives

Use the Current Folder browser to view the list of files in a folder.

More About

expand all

Tips

  • 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.

  • Short DOS file name support

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

  • Structure Results for Nonexistent Files

    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, then 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, which points to a nonexistent target. A nonexistent target is a target that was moved, removed, or renamed. For example, if my_file in my_dir is a symbolic link to another file that was deleted, then running

    r = dir('my_dir')

    includes this result for my_file:

    r(n) = 
    		name: 'my_file'
    		date: ''
    		bytes: []
    		isdir: 0
    		datenum: []
    

    where n is the index for my_file, found by searching r by the name field.

See Also

| | | | | |

Was this topic helpful?