WolframLang: File Path Functions
Dir Path Separator
Note: WolframLang path functions use a dir separator compatible to your operating system. On linux and Mac, it's slash, on Microsoft Windows its blackslash. But blackslash in WolframLang string is a escape character, so, it needs to be double backslash.
Note: Microsoft Windows supports slash for dir path. So, you can always use slash for input.
Get Dir Path
DirectoryName
-
DirectoryName[path]
return the directory path.
(* tested on Microsoft Windows *) DirectoryName[ "c:/a/b/c.html" ] (* c:\a\b\ *)
FileNameDrop[path]
-
drop the last part of path.
FileNameDrop[ "c:/a/b/c.html" ] (* c:\a\b *) FileNameDrop[ "c:/a/b/" ] (* c:\a *)
Get File Name Part of Path
FileNameTake[path]
-
return the filename part.
FileNameTake[ "c:/Users/xah/.emacs.d/temp/x20241205_1038_4eb.wl" ] (* x20241205_1038_4eb.wl *)
Get File Base Name
FileBaseName[path]
-
return the file base name. (i.e. sans dir and sans file extension)
FileBaseName["c:/Users/xah/abc.txt"] (* abc *)
Get File Name Extension
FileExtension[path]
-
return the file extension. (without the dot)
FileExtension["c:/a/b/c.txt"] (* txt *)
Get File Path Depth
FileNameDepth[path]
-
return the depth of file path.
FileNameDepth["c:/Users/xah/abc.txt"] (* 4 *) FileNameDepth["c:\\Users\\xah\\abc.txt"] (* 4 *) (* pathological example *) FileNameDepth["c:///abc.txt"] (* 4 *) (* it basically count the number of separators plus 1 *)
Split File Path to List
FileNameSplit[path]
-
split the file path into a list.
FileNameSplit["c:/a/b"] (* {c:, a, b} *) FileNameSplit["c:\\a\\b"] (* {c:, a, b} *) FileNameSplit["/a/b"] (* {, a, b} *) (* edge case *) FileNameSplit["c:/a/b//c"] (* {c:, a, b, , c} *)
Join Path
FileNameJoin[dir, name]
-
join directory and file name into a single path.
(* all example tested on Microsoft Windows *) FileNameJoin[ {"a","b","c"} ] (* a\b\c *) FileNameJoin[ {"a/","b"} ] (* a\b *) (* dir separator is added if none exist *) FileNameJoin[ {"a","b"} ] (* a\b *) (* extra dir separator is removed *) FileNameJoin[ {"a/","/b"} ] (* a\b *) (* extra dir separator is removed *) FileNameJoin[ {"a//","b"} ] (* a\b *)
Expand File Path (Relative Path to Full)
ExpandFileName[name]
-
return full path (name relative to current directory).
ExpandFileName[ "c.html" ] (* C:\Users\xah\.emacs.d\temp\c.html *) (* on Microsoft Windows *) (* basically prepend dir path to arg *)
Get Absolute File Path
AbsoluteFileName[name]
-
return a absolute full path.
file must exist
(* tested on Microsoft Windows *) (* relative name to full path *) AbsoluteFileName[ "xx.py" ] (* C:\Users\xah\.emacs.d\temp\xx.py *) AbsoluteFileName[ "c:/Users/xah/web/xahlee_info/M/index.html" ] (* C:\Users\xah\web\xahlee_info\M\index.html *) (* if file no exist, return $Failed *) AbsoluteFileName[ "xxx" ] (* $Failed *)
WolframLang, Package, Load File
WolframLang, Shell Scripting Tasks
- WolframLang: Shell Scripting Tasks
- WolframLang: Navigate Directory
- WolframLang: List Files (Walk Directory)
- WolframLang: File Path Functions
- WolframLang: Delete Dir
- WolframLang: is File or Dir
- WolframLang: File Exist
- WolframLang: Get File Date
- WolframLang: Get File Size
- WolframLang: Print File Content
- WolframLang: Get File Hash
- WolframLang: Read File
- WolframLang: Read File from Web
- WolframLang: Write File
- WolframLang: Load a Package or File
- WolframLang: Get Environment Variable
- WolframLang: Date Time
- WolframLang: Find Replace Script