Is there a way to get the script directory from inside a Mathematica script?
I want a function analogue to NotebookDirectory[]
(which doesn't work for scripts). Is there a way to do this?
DirectoryName @ $InputFileName
is the answer, right?
And this will work whether you call it from a package or a notebook interface:
parentPath = $InputFileName /. "" :> NotebookFileName[]
parentDir = DirectoryName @ parentPath
Check:
path = FileNameJoin[{$TemporaryDirectory, "test.m"}];
Export[
path,
"Print @ DirectoryName @ $InputFileName",
"Text"
]
Get @ path
(...)\Local\Temp\
wolframscript
$\endgroup$
Print["test parent dir: ", DirectoryName @ $InputFileName]
I get: i.sstatic.net/SrLrA.png
$\endgroup$
wolfram -script script.m
it works. But if I do wolframscript -file script.m
it doesn't work. I am on Mathematica 11.1.1 Kernel for Linux x86 (64-bit).
$\endgroup$
wolframscript
?
$\endgroup$