dir - List folder contents (2024)

List folder contents

collapse all in page

Syntax

dir

dir name

listing = dir(name)

Description

dir lists files and folders in the currentfolder.

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

View Contents of Folder

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.

myfile2.ext

Search Subfolders

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

Suppose that the current folder has these contents.

file1.extfolder2 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 

Read Details

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

nameFile or folder name
string scalar | character vector

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

listing — File attributes
structure array

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

You can read data from primary online sources by performing file read operations over an internet URL.

Starting in R2020a, on UNIX® platforms, the wildcard expression *.* no longer matches folders or files without an extension. In previous releases, the expression matches folders or files regardless of extension, including files without an extension. This change of behavior does not apply to Microsoft Windows platforms.

See Also

cd | fileattrib | isfolder | ls | mkdir | rmdir | what

Topics

  • Specify File Names
  • Work with Remote Data

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

dir - List folder contents (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

dir - List folder contents (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5841

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.