How to | Input and Construct File Names in the Wolfram Language
The Wolfram Language provides a simple and consistent method for accessing and using files. In addition to inserting a file path that is specific to your operating system, the Wolfram Language also allows programmatic construction of directory and file paths that are portable across different operating systems.
The Wolfram System lets you insert directory and file paths by typing them in directly, using the Insert ▶ File Path menu item, or with a keystroke-modified drag and drop. For information on these methods, see How to: Insert a File Path.
If instead you were to use StringSplit or StringJoin to work with file names, you would need to manually replace the pathname separators and other file name conventions for each operating system you want your program to run on.
Use FileNameJoin to construct a path for a hypothetical file on Windows:
FileNameJoin also works with file paths that contain pathname separators:
You do not have to provide a file for FileNameJoin, since it also works with directories:
FileNameJoin is very useful for constructing file names and paths based on Wolfram Language system variables that represent certain directories.
Use FileNameSplit to split a file path into its components:
Use FileNameSplit to split $UserBaseDirectory into its components. The first element of this list is an empty string. This represents the fact that $UserBaseDirectory begins with a file name separator:
Use [[...]] (the short form of Part) to get the first 4 directory components:
Use FileNameJoin to construct a directory path from these components by using % (which specifies the most recent output):
You could also construct the same path by using FileNameDrop: