DirectoryName["name"]
extracts the directory name from the specification for a file.


DirectoryName
DirectoryName["name"]
extracts the directory name from the specification for a file.
Details and Options

- DirectoryName works differently on different computer systems.
- DirectoryName["directory"] is normally equivalent to ParentDirectory["directory"].
- DirectoryName["name",n] applies DirectoryName n times to name.
- DirectoryName yields output appropriate for use in SetDirectory and FileNameJoin.
- If name contains no directory specification, DirectoryName["name"] returns "".
- DirectoryName by default uses pathname separators and other conventions suitable for your operating system.
- DirectoryName[…,OperatingSystem->"os"] uses the conventions of the specified operating system. Possible choices are "Windows", "MacOSX", and "Unix".
- DirectoryName[File["name"]] is also supported.
Examples
open all close allBasic Examples (1)
Get the directory name of a file on Windows:
Get the directory name of a file on Mac OS X:
Apply DirectoryName two times:
Scope (1)
Get the directory name of a file specified as a File object:
See Also
Directory ParentDirectory SetDirectory FileBaseName FileNameJoin $Path $PathnameSeparator $Input $InputFileName NotebookDirectory
Function Repository: DirectorySize
Tech Notes
Related Guides
Related Workflows
- Construct a File Path Programmatically
History
Introduced in 1996 (3.0) | Updated in 2008 (7.0) ▪ 2016 (11.0)
Text
Wolfram Research (1996), DirectoryName, Wolfram Language function, https://reference.wolfram.com/language/ref/DirectoryName.html (updated 2016).
CMS
Wolfram Language. 1996. "DirectoryName." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/DirectoryName.html.
APA
Wolfram Language. (1996). DirectoryName. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DirectoryName.html
BibTeX
@misc{reference.wolfram_2025_directoryname, author="Wolfram Research", title="{DirectoryName}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/DirectoryName.html}", note=[Accessed: 14-August-2025]}
BibLaTeX
@online{reference.wolfram_2025_directoryname, organization={Wolfram Research}, title={DirectoryName}, year={2016}, url={https://reference.wolfram.com/language/ref/DirectoryName.html}, note=[Accessed: 14-August-2025]}