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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityIs 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$
– Santiago
Aug 22 '17 at 13:31
Print["test parent dir: ", DirectoryName @ $InputFileName]
I get: i.stack.imgur.com/SrLrA.png
$\endgroup$
– Kuba♦
Aug 22 '17 at 13:41
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$
– Santiago
Aug 22 '17 at 14:29
wolframscript
?
$\endgroup$
– Santiago
Aug 23 '17 at 8:31