Useful functions in this context:
Split the path in components:
FileNameSplit["U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf"]
(* {"U", "SomeDirectory", "SomeSecret Directory", "veryImportantFile.pdf"} *)
Get the 'pure' file name (without file extension):
FileBaseName["U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf"]
(* "veryImportantFile" *)
Get the name, including the file extension:
FileNameTake["U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf"]
(* "veryImportantFile.pdf" *)
Get the extension itself:
FileExtension["U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf"]
(* "pdf" *)
Get the directory only:
FileNameDrop["U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf"]
(* "U\\SomeDirectory\\SomeSecretDirectory" *)
or by:
DirectoryName["U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf"]
(* "U\\SomeDirectory\\SomeSecretDirectory\\" *)
[Note the latter includes a final slash, whereas the former doesn't]
The inverse of FileNameSplit
is FileNameJoin
. It is an important function as it makes sure to use the path separators that work on the OS the user is working on and it makes file paths system independent.
No option (Mathematica automatically chooses OS):
FileNameJoin[{"U", "SomeDirectory", "SomeSecret Directory", "veryImportantFile.pdf"}]
(* "U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf" *)
OS choice made explicitly:
FileNameJoin[{"U", "SomeDirectory", "SomeSecret Directory", "veryImportantFile.pdf"},
OperatingSystem -> #
] & /@ {"Windows", "Unix", "MacOSX"}
(* {"U\\SomeDirectory\\SomeSecretDirectory\\veryImportantFile.pdf",
"U/SomeDirectory/SomeSecretDirectory/veryImportantFile.pdf",
"U/SomeDirectory/SomeSecretDirectory/veryImportantFile.pdf"} *)
FileNameTake
,FileBaseName
andFileExtension
,DirectoryName
might also be of interest... $\endgroup$