fileparts

Get parts of file name

Description

example

[filepath,name,ext] = fileparts(filename) returns the path name, file name, and extension for the specified file.

fileparts only parses the specified filename. It does not verify that the file exists.

Examples

collapse all

file = 'H:\user4\matlab\myfile.txt';
[filepath,name,ext] = fileparts(file)
filepath = 
'H:\user4\matlab'
name = 
'myfile'
ext = 
'.txt'

Get the parts of a user .cshrc file name for a Linux® system.

fileparts interprets the entire file name as an extension because it begins with a period.

[filepath,name,ext] = fileparts('/home/jsmith/.cshrc')
filepath = 
'/home/jsmith'
name =

  1x0 empty char array
ext = 
'.cshrc'

Input Arguments

collapse all

File name, specified as a string scalar or character vector. filename can include a path and file extension.

On Microsoft® Windows® systems, you can use either forward slashes (/) or backslashes (\) as path delimiters, even within the same file name. On UNIX® and Macintosh systems, use only / as a delimiter.

Data Types: char | string | cell

Output Arguments

collapse all

File path, returned as a string scalar or character vector. filepath has the same data type as the input argument filename. If the name of the file to parse does not specify a path, filepath is empty ('').

Data Types: char | string

File name, returned as a string scalar or character vector. name has the same data type as the input argument filename.

The extension is not included. fileparts interprets all characters following the rightmost delimiter as the name of the file plus extension.

Data Types: char | string

File extension, returned as a string scalar or character vector. ext has the same data type as the input argument filename.

ext begins with a period (.). If the name of the file to parse does not specify an extension, ext is empty ('').

Data Types: char | string

Tips

  • To reconstruct a file name from the output of fileparts, use strcat to concatenate the file name and the extension that begins with a period (.) without a path separator. Then, use fullfile to build the file name with the platform-dependent file separators where necessary. For example, fullfile(filepath, strcat(name,ext)).

  • To specify a folder name only, be sure that the rightmost character in filename is a delimiter (/ or \). Otherwise, fileparts parses the trailing portion of filename as the name of the file and returns it in name instead of in filepath.

Introduced before R2006a