RunMat
GitHub

dir — Return file and folder information in a MATLAB-compatible struct array.

dir returns metadata for files and folders. Without arguments, it lists the current working directory. With an argument, it can list a specific directory, match wildcard patterns, or report information about a single file. The result is a column vector of structs matching MATLAB's signature: each element contains name, folder, date, bytes, isdir, and datenum fields.

How dir works in RunMat

  • dir with no inputs returns the contents of the current folder (including . and ..).
  • When you call dir without capturing the output, RunMat prints the listing in the REPL. Assigning the result to a variable suppresses the listing and returns the struct array.
  • dir(path) accepts absolute or relative paths, character vectors, or string scalars. When the path names a directory, its contents are listed; when it names a file, metadata for that file alone is returned.
  • dir(pattern) and dir(folder, pattern) support wildcard expansion with * and ?. Patterns are evaluated relative to the supplied folder or the current working directory.
  • The returned struct fields mirror MATLAB:
  • name: file or folder name.
  • folder: absolute path to the containing folder.
  • date: last-modified timestamp formatted as dd-MMM-yyyy HH:mm:ss in local time.
  • bytes: file size in bytes (0 for directories and wildcard-only results).
  • isdir: logical flag indicating whether the entry is a directory.
  • datenum: serial date number compatible with MATLAB's datenum.
  • Results are sorted using MATLAB-style ordering (case-insensitive on Windows, case-sensitive on Unix-like systems).
  • Empty matches return a 0×1 struct array so idioms like isempty(dir("*.tmp")) continue working.
  • Invalid arguments (numeric, logical, non-scalar string arrays) raise MATLAB-compatible diagnostics.

How dir runs on the GPU

Because dir interacts with the host filesystem, it is executed entirely on the CPU. If an input argument resides on the GPU (for example, a scalar string created by another accelerated builtin), RunMat gathers it to host memory before expanding patterns. Acceleration providers do not implement hooks for dir, and the result always lives on the host.

GPU memory and residency

No. dir is an I/O-bound builtin and always operates on host data. Passing GPU-resident strings is supported, but RunMat gathers them automatically and there is no benefit to manually calling gpuArray.

Examples

List Files In The Current Folder

fid = fopen("README.md", "w"); fclose(fid);
mkdir("src");
listing = dir;
{listing.name}

Expected output:

{'.'}    {'..'}    {'README.md'}    {'src'}

List Only MATLAB Files Using Wildcards

fid = fopen("solver.m", "w"); fclose(fid);
fid = fopen("test_helper.m", "w"); fclose(fid);
scripts = dir("*.m");
cellstr({scripts.name})

Expected output:

{'solver.m'}
    {'test_helper.m'}

Capture Metadata For A Specific File

info = dir("data/results.csv");
info.bytes    % file size in bytes
info.isdir    % false

List The Contents Of A Specific Folder

mkdir("tmp");
fid = fopen(fullfile("tmp", "tmpfile.txt"), "w"); fclose(fid);
mkdir(fullfile("tmp", "subdir"));
tmp = dir(fullfile(pwd, "tmp"));
{tmp.name}'

Expected output:

'.'    '..'    'tmpfile.txt'    'subdir'

Combine Folder And Pattern Arguments

mkdir("assets");
fid = fopen(fullfile("assets", "logo.png"), "w"); fclose(fid);
fid = fopen(fullfile("assets", "splash.png"), "w"); fclose(fid);
images = dir("assets", "*.png");
{images.name}

Expected output:

{'logo.png'}    {'splash.png'}

Use Tilde Expansion For Home Directory

home_listing = dir("~")

Expected output:

home_listing(1).folder
ans =
    '/'

Handle Missing Matches Gracefully

if isempty(dir("*.cache"))
    disp("No cache files found.");
end

Expected output:

No cache files found.

FAQ

What types can I pass to dir?

Character vectors or string scalars. Other types (numeric, logical, multi-element string arrays, or cells) raise an error.

Does dir support recursive wildcards?

Yes. Patterns such as "**/*.m" are honoured through the standard globbing rules.

Why do I see . and .. entries?

MATLAB includes them for directory listings; RunMat mirrors this behaviour so scripts relying on their presence continue to work.

What is the datenum field?

A MATLAB serial date number representing the last modification time in local time. Use datetime([entry.datenum]) to convert multiple entries.

Symlinks are reported using the metadata provided by the operating system. If the link targets a directory, isdir is true.

Can I pass GPU-resident strings?

Yes, but RunMat gathers them automatically before computing the directory listing.

How are errors reported?

Error messages are prefixed with dir: and match MATLAB's argument diagnostics wherever possible.

What does dir do in MATLAB?

dir lists files and folders in the current directory. dir(name) lists items matching a file or folder name, which can include wildcards like '*.m'.

How do I list all files in a folder in MATLAB?

Use files = dir('foldername') to get a struct array. Each element has fields name, folder, bytes, isdir, datenum, and date. Filter with files(~[files.isdir]) to exclude folders.

Use dir('**/*.m') with the double-star wildcard to recursively find all .m files in subdirectories. This syntax works in RunMat.

These functions work well alongside dir. Each page has runnable examples you can try in the browser.

ls, pwd, cd, addpath, copyfile, delete, exist, fullfile, genpath, getenv, mkdir, movefile, path, rmdir, rmpath, savepath, setenv, tempdir, tempname

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how dir works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.