List folder contents - MATLAB dir (2024)

List folder contents

collapse all in page

Syntax

dir

dir name

listing = dir(name)

Description

dir lists files and folders in the currentfolder.

example

dir name lists files and folders that match 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.

example

listing = dir(name) returnsattributes about name.

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 myfoldermovefile myfile1.m myfoldermovefile myfile2.m myfoldermovefile 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 myfoldermovefile myfile1.m myfoldermovefile myfile2.m myfoldermovefile myfile3.txt myfolder

List the matching files in myfolder.

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.mmyfolder2 myfile2.m myfolder3 myfile3.m
mkdir myfolder1mkdir myfolder1/myfolder2mkdir myfolder1/myfolder2/myfolder3movefile myfile1.m myfolder1movefile myfile2.m myfolder1/myfolder2movefile myfile3.m myfolder1/myfolder2/myfolder3

List all files with a .m extension in myfolder1 and all of the subfolders of myfolder1.

cd myfolder1dir **/*.m
Files Found in Current Folder:myfile1.m Files Found in: myfolder2myfile2.m Files Found in: myfolder2\myfolder3myfile3.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 myfoldermovefile myfile1.m myfoldermovefile myfile2.m myfoldermovefile 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 date and time a file was last modified.

First, query the datenum field of the structure that dir returns. The value of the datenum field is a serial date number and does not vary with locale.

MyFileInfo = dir('myfile1.m');FileDate = MyFileInfo.datenum

As of R2022b, serial date numbers are not recommended. Convert the serial date number to a datetime value by using the datetime function.

FileDatetime = datetime(FileDate,ConvertFrom="datenum")
FileDatetime = datetime 24-May-2016 11:24:31

Input Arguments

collapse all

nameFile or folder name
character vector | string scalar

File or folder name, specified as a character vector or string scalar. If name is a string, enclose it in parentheses. For example, dir("FolderName").

To list files and folders at a remote location, name must contain a full path specified as a uniform resource locator (URL). For more information, see Work with Remote Data.

To search for multiple files, use wildcards in the file name. For example, dir *.txt lists all files with a txt extension in the current folder. To search through folders and subfolders on the path recursively, use wildcards in the path name. For example, dir */*.txt lists all files with a txt extension exactly one folder under the current folder, and dir **/*.txt lists all files with a txt extension zero or more folders under the current folder. Characters next to a ** wildcard must be file separators.

Note

MATLAB® always treats the * character as a wildcard, even on file systems that support * in file names.

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

Output Arguments

collapse all

listing — File attributes
structure array

File attributes, returned as a n-by-1 structurearray, where n is the number of files and foldersreturned by the dir command.

This table shows the fields in the structure.

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 ifname is a file

logical

datenum

Modification date as serial date number.

double

Tips

  • To exclude invalid entries returned by the dir command,use the cellfun function.

    MyFolderInfo = dir; MyFolderInfo = MyFolderInfo(~cellfun('isempty', {MyFolderInfo.date})); 

    Invalid entries occur when you run dir withan output argument and the results include a nonexistent file or afile that dir cannot query for some other reason.In this case, dir returns the following defaultvalues.

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

    Invalid entries most commonly occur on UNIX® platforms when dir queriesa symbolic link pointing to a nonexistent target. A nonexistent targetis 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.

Extended Capabilities

Version History

Introduced before R2006a

expand all

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.

List folder contents - MATLAB dir (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

List folder contents - MATLAB dir (2024)

FAQs

List folder contents - MATLAB dir? ›

To search through folders and subfolders on the path recursively, use wildcards in the path name. For example, dir */*. txt lists all files with a txt extension exactly one folder under the current folder, and dir **/*. txt lists all files with a txt extension zero or more folders under the current folder.

How do I make a list of folder contents? ›

Using COMPUTER or WINDOWS EXPLORER navigate to the folder containing the files you want to make a list of. o Do not open the folder– you should be 'one level' up so you see the folder itself and not the contents. Press and hold the SHIFT key and then right-click the folder that contains the files you need listed.

How to get a list of all folders in a directory? ›

Steps
  1. Open File Explorer in Windows. ...
  2. Click in the address bar and replace the file path by typing cmd then press Enter.
  3. This should open a black and white command prompt displaying the above file path.
  4. Type dir /A:D. ...
  5. There should now be a new text file called FolderList in the above directory.

What does list folder contents? ›

List folder contents: Permits viewing and listing of files and subfolders as well as executing of files; inherited by folders only. Read: Allows users to view the folder and subfolder contents. Write: Allows users to add files and subfolders, allows you to write to a file.

How do I get a list of text contents of a folder? ›

Print file list to a txt file
  1. In File Explorer, go to the location of the folder in which you want to print a contents list.
  2. Press Alt -> D on your keyboard (the address bar of Files Explorer will now be in focus).
  3. Type cmd and press Enter. ...
  4. Copy and paste the following to the command prompt:
  5. dir > files_and_folders.txt.
Jul 12, 2022

How do I show the contents of a folder? ›

-
  1. To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) ...
  2. To display detailed information, type the following: ls -l chap1 .profile. ...
  3. To display detailed information about a directory, type the following: ls -d -l .

How do I print the entire contents of a folder? ›

Printing All Documents in a Folder
  1. From the folder browser, select the folders whose contents you want to print.
  2. From the File menu, click Print, or click (Print) from the toolbar.
  3. From the Print what drop-down, select what will be printed from the documents.

How to get list of files in a folder in cmd? ›

To do this, simply type the “dir” command and press Enter. This command displays a list of files and directories in the current directory.

How to list only files in dir command? ›

dir >..\myfile.txt

Adding /b to the command causes the list to contain just the file and directory names, not the information on the number of files, when they were created, etc.. Adding /a-d to the command removes the names of the directories, so all we have are the file names.

How do I get the list of all files in a folder and subfolders on a Mac? ›

The ls command lists all the files that are stored inside the folder. The –R command expands out subdirectories and lists the files contained within them.

What is used to display a directory list of all files folders? ›

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

What are directory contents? ›

Directories contain directory entries. Each entry contains a file or subdirectory name and an index node reference number (i-node number). To increase speed and enhance use of disk space, the data in a file is stored at various locations in the memory of the computer.

What displays the contents of a folder? ›

The navigation pane displays the contents of the file or folder selected​

How do I turn the contents of a file into a list? ›

Explanation First, we open the file in the read mode using the open(); then we use the split function. Inside the split function, we have used the comma(,) as the separator and passed it into the split() method argument. This will create a list from the file's data.

How do I extract the contents of a folder? ›

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All..., and then follow the instructions.

How can I print a list of the contents of any folder? ›

Open File Explorer, and go to the folder containing your files. Click the File menu in the top left corner and select Print. Select Print from the drop-down menu. Click the Print button and your list of files will be printed.

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5261

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.