dir
List folder contents
Description
dir
lists files and folders in the current
folder.
dir
lists files and folders
that match name
name
. When name
is a folder,
dir
lists the contents of the folder. Specify
name
using absolute or relative path names. The
name
argument can include the *
wildcard in the file name, and both the *
and the
**
wildcard in the path name. Characters next to a
**
wildcard must be file separators.
Examples
View Contents of Folder
List the contents of a folder.
Create a folder, myfolder
, that contains the files myfile1.m
, myfile2.m
, and myfile3.m
.
mkdir myfolder movefile myfile1.m myfolder movefile myfile2.m myfolder movefile myfile3.m myfolder
List the files in myfolder
.
dir myfolder
. .. myfile1.m myfile2.m myfile3.m
Find Files Matching Specified Name
List all files with a .m
extension that contain the term my
.
Create a folder, myfolder
, that contains the files myfile1.m
, myfile2.m
, and myfile3.txt
.
mkdir myfolder movefile myfile1.m myfolder movefile myfile2.m myfolder movefile myfile3.txt myfolder
List the matching files in myfolder
.
cd myfolder dir *my*.m
myfile1.m myfile2.m
Find Files in Subfolders
List all files in the current folder and all of the subfolders of the current folder.
Create a folder, myfolder1
, that contains these files and
folders:
myfile1.m myfolder2 myfile2.m myfolder3 myfile3.m
mkdir myfolder1 mkdir myfolder1/myfolder2 mkdir myfolder1/myfolder2/myfolder3 movefile myfile1.m myfolder1 movefile myfile2.m myfolder1/myfolder2 movefile myfile3.m myfolder1/myfolder2/myfolder3
List all files with a .m
extension in
myfolder1
and all of the subfolders of
myfolder1
.
cd myfolder1 dir **/*.m
Files Found in Current Folder: myfile1.m Files Found in: myfolder2 myfile2.m Files Found in: myfolder2\myfolder3 myfile3.m
Find Information in the Return Structure
Return the folder listing of myfolder
to the variable MyFolderInfo
.
Create a folder, myfolder
, that contains the files myfile1.m
, myfile2.m
, and myfile3.m
.
mkdir myfolder movefile myfile1.m myfolder movefile myfile2.m myfolder movefile myfile3.m myfolder
Get a list of the files in myfolder
. MATLAB® returns the information in a structure array.
MyFolderInfo = dir('myfolder')
MyFolderInfo=5×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
Index into the structure to access a particular item.
MyFolderInfo(3).name
ans = 'myfile1.m'
Find Date File Last 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. Do not use the datenum
function to convert 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.
MyFileInfo = dir('myfile1.m');
FileDate = MyFileInfo.datenum
FileDate = 7.3647e+05
Input Arguments
Output Arguments
Tips
To exclude invalid entries returned by the
dir
command, use thecellfun
function.MyFolderInfo = dir; MyFolderInfo = MyFolderInfo(~cellfun('isempty', {MyFolderInfo.date}));
Invalid entries occur when you run
dir
with an output argument and the results include a nonexistent file or a file thatdir
cannot query for some other reason. In this case,dir
returns the following default values.date: '' bytes: [] isdir: 0 datenum: []
Invalid entries most commonly occur on UNIX® platforms when
dir
queries a symbolic link pointing to a nonexistent target. A nonexistent target is a target that is moved, removed, or renamed.To obtain a list of available drives on Microsoft Windows platforms, use the DOS
net use
command at the command line.dos('net use')
Or type
[s,r] = dos('net use')
MATLAB returns the results to the character array
r
.