FileNameTake

FileNameTake["name"]

gives the last path element in the file name "name".

FileNameTake["name",n]

gives the first n path elements in the file name "name".

FileNameTake["name",-n]

gives the last n path elements in the file name "name".

FileNameTake["name",{m,n}]

gives elements m through n in the file name "name".

Details and Options

  • FileNameTake by default assumes that path elements are separated by pathname separators suitable for your operating system.
  • FileNameTake[,OperatingSystem->"os"] uses the conventions of the specified operating system. Possible choices are "Windows", "MacOSX", and "Unix".
  • FileNameTake just operates on names of files; it does not actually search for the file specified.
  • If more elements are requested than are present, FileNameTake just gives those that are present.
  • FileNameTake[File["name"],] is also supported.

Examples

open allclose all

Basic Examples  (4)

Extract the last path element from a file name:

In[1]:=
Click for copyable input
Out[1]=

Get different path elements in a file name:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=

Find out where the Wolfram System is installed:

In[1]:=
Click for copyable input
Out[1]=

On Windows, the share name is treated as a single unit:

In[1]:=
Click for copyable input
Out[1]=

Scope  (1)

See Also

DirectoryName  FileNameSplit  FileBaseName  FileNameDrop  FileNameDepth  FileExtension  StringTake  $OperatingSystem

Introduced in 2008
(7.0)
| Updated in 2016
(11.0)