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
.
cd myfolderdir *2*.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
name
— File 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 When |
|
|
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 |
|
folder | Location of file or folder |
|
date | Modification date timestamp |
|
bytes | Size of the file in bytes |
|
isdir |
|
|
datenum | Modification date as serial date number |
|
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
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
expand all
R2024b: Read data over HTTP and HTTPS using file operation functions
You can read data from primary online sources by performing file read operations over an internet URL.
R2020a: Wildcard expression *.*
on UNIX platforms matches only files that have an extension
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.
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)
Contact your local office