fileparts

Splits a file path into folder, name and extension.

Category: Programming > Files and Folders

Syntax

[folder, name, ext] = fileparts(filename)

Returns the three parts of filename.

Description

fileparts works on the text only and never touches the disk. The extension starts at the last dot of the file name, so 'archive.tar.gz' gives name 'archive.tar' and extension '.gz'. A name that starts with a dot and has no other dot, such as '.hidden', is returned as an extension with an empty name.

Both / and \ are accepted as separators. The output class follows the input: a string gives strings. fullfile does the reverse.

Input arguments

filename
Path as text. The file does not need to exist.

Output arguments

folder
Everything before the last file separator, without the separator.
name
File name without the extension.
ext
The extension, including the leading dot; empty if there is none.

Example

Splitting a path

[folder, name, ext] = fileparts('C:\data\run3\results.tar.gz')
[~, n, e] = fileparts('notes')
Output
folder = 'C:\data\run3'
name = 'results.tar'
ext = '.gz'
n = 'notes'
e = 0×0 empty char array

See also

fullfile filesep fileread exist