WolframLang: File Path Functions

By Xah Lee. Date: . Last updated: .

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

FileNameDrop[path]

return the directory part.

FileNameDrop

FileNameDrop[ "c:/a/b/c.html" ]
(* c:\a\b *)
(* on Microsoft Windows *)

Get File Name Part of Path

FileNameTake[path]

return the filename part.

FileNameTake

FileNameTake[ "c:/a/b/c/d.html" ] === "d.html"

Get File Base Name

FileBaseName[path]

return the file base name. (without file extension)

FileBaseName

FileBaseName["c:/Users/xah/abc.txt"] === "abc"

Get File Name Extension

FileExtension[path]

return the file extension. (without the dot)

FileExtension

FileExtension["c:/a/b/c.txt"] === "txt"

Get File Path Depth

FileNameDepth[path]

return the depth of file path.

FileNameDepth

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

(* all result on Microsoft Windows *)

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.

FileNameJoin

FileNameJoin[ {"a","b","c"} ] === "a\\b\\c"
(* on Microsoft Windows *)

Expand File Path (Relative Path to Full)

ExpandFileName[name]

return full path (name relative to current directory).

ExpandFileName

ExpandFileName[ "c.html" ]
(* C:\Users\xah\.emacs.d\temp\c.html *)
(* on Microsoft Windows *)
(* basically prepend current dir path to arg *)

Get Absolute File Path

AbsoluteFileName[name]

return a absolute full path.

AbsoluteFileName

AbsoluteFileName[ "c:/Users/xah/web/xahlee_info/M/index.html" ]
(* C:\Users\xah\web\xahlee_info\M\index.html *)
(* on Microsoft Windows *)
(* file must exist *)

WolframLang: Shell Tasks