Main Content

dir

List folder contents

Description

dir lists files and folders in the current folder.

dir name lists files and folders that match name. When name is a folder, dir lists the contents of the folder. Use wildcards * and ** to match patterns. For example, dir *.mlx lists all files with the extension .mlx, and dir ** includes files in all subfolders.

example

listing = dir(name) returns attributes in a structure array, such as when each file or folder was last modified.

example

Examples

collapse all

Suppose that myfolder is in the current working folder and contains three files. List its contents.

dir myfolder
.          ..         myfile1.ext  myfile2.ext  myfile3.ext

Go to myfolder and list the files with 2 in the filename and the extension .ext.

cd myfolder
dir *2*.ext
myfile2.ext

List files in the current folder and its subfolders using the recursive search wildcard, **.

Suppose that the current folder has these contents.

file1.ext
folder2
     file2.ext
     folder3
         file3.ext

List all files in the hierarchy.

dir **
Files Found in Current Folder:

.          ..         file1.ext  folder2  

Files Found in: folder2

.          ..         file2.ext  folder3  

Files Found in: folder2/folder3

.          ..         file3.ext  

When processing details about multiple files, it is often helpful to convert the output structure to a table.

Suppose that the current folder has these contents.

file1.ext
folder2
     file2.ext
     folder3
         file3.ext

Request details for the entire hierarchy using the ** wildcard. The output is a structure array. The date field is of type char, and the datenum field is a serial date number.

listing = dir("**")
listing = 

  11x1 struct array with fields:

    name
    folder
    date
    bytes
    isdir
    datenum

Convert the structure to a table, and replace date with datetime values.

tbl = struct2table(listing);
tbl.date = datetime(tbl.datenum,ConvertFrom="datenum");
tbl = removevars(tbl,"datenum")
tbl = 

  11x5 table

        name               folder                   date            bytes    isdir  
    _____________    ___________________    ____________________    _____    _____

    {'.'        }    {'/mycurrent'     }    01-Mar-2024 15:45:35      0      true
    {'..'       }    {'/mycurrent'     }    01-Mar-2024 15:44:10      0      true
    {'file1.ext'}    {'/mycurrent'     }    01-Mar-2024 15:45:45      8      false
    {'folder2'  }    {'/mycurrent'     }    01-Mar-2024 15:45:16      0      true 
    .
    .
    .

To find subsets of the data, index into the table. For instance, extract only the folders, and then exclude the . and .. folders.

folders = tbl(tbl.isdir,:);
namedFolders = folders(~matches(folders.name,[".",".."]),:)
namedFolders =

  2x5 table

        name                folder                  date             bytes    isdir
    ___________    ______________________    ____________________    _____    _____

    {'folder2'}    {'/mycurrent'        }    01-Mar-2024 15:45:16      0      true 
    {'folder3'}    {'/mycurrent/folder2'}    01-Mar-2024 15:44:58      0      true 

Input Arguments

collapse all

File or folder name with the full or relative path, specified as a string scalar or character vector.

For files and folders at a remote location, you must specify the full path as a uniform resource locator (URL). Internet URLs must include the protocol type "http://" or "https://". For more information, see Work with Remote Data.

To search for patterns or to search multiple folders, use wildcards * or **. The dir function always treats the * character as a wildcard, even on file systems that support * in filenames.

Wildcard

Meaning

*

When * is part of a file or folder name, it represents any number of characters. For example, dir *.mat lists all MAT files in the current folder.

When * is in the path and only next to file separators, it represents a single level of hierarchy. For example, dir */*.mat lists MAT files exactly one folder under the current folder.

**

** represents a recursive search through subfolders. For example, dir ** lists files in the current folder and all levels of its subfolders. If you specify characters next to a ** wildcard, they must be file separators.

On Microsoft® Windows® systems, dir supports short filenames generated by DOS.

Output Arguments

collapse all

File attributes, returned as an n-by-1 structure array, where n is the number of files and folders that match name.

The structure contains these fields.

Field Name

Description

Class

name

File or folder name

char

folder

Location of file or folder

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

For internet URLs, if file size information is not available, the bytes field will be NaN.

Limitations

  • MATLAB® does not support internet URLs that require authentication.

  • MATLAB Online™ supports internet URLs associated with Microsoft OneDrive™ files and folders, while the installed version of MATLAB supports only local OneDrive files.

Tips

  • If dir cannot query a file, it returns these default values in the output structure.

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

    This issue most commonly occurs when querying a symbolic link pointing to a moved, removed, or renamed target. To exclude these invalid entries, you can convert the structure to a table and remove the rows with empty values.

    listing = struct2table(dir); 
    listing(isempty(listing.bytes),:) = [];
    
  • To obtain a list of available drives on Microsoft Windows platforms, use the DOS net use command.

    dos("net use")

Extended Capabilities

Version History

Introduced before R2006a

expand all