I want to get the current working directory, or the directory/path where the script being called when using wolframscript. Without frontend, i.e. not using NotebookDirectory.

Because when I want to get the current directory by NotebookDirectory in wolframscript, it shows:

FrontEndObject::notavail: A front end is not available; certain operations require a front end.


With the help of @Edmund, I use Directory to solve this problem.

But it introduced another problem. If I use Directory in package files, the notebook cannot use the package, because Directory returns home path. But if I use NotebookDirectory, the wolframscript cannot use the package, because NotebookDirectory uses frontend. How can I solve this?


With the help of @MB1965 If[$InputFileName="", NotebookDirectory[], Directory[]] will solve the above problem.

Further, If I use

ppNoteBookDir = If[$BatchInput, Directory[], NotebookDirectory[]];
Needs["msgCommon`", FileNameJoin[{ppNoteBookDir, "msg_common.wl"}]]

Then the first time I evaluate the notebook, it shows: enter image description here

It need evaluate the notebook a second time to work right. Why and how to fix it? Thanks.

  • $\begingroup$ @cmal I marked this topic a duplicate, let me know if you disagree, I'm open to discussion. $\endgroup$
    – Kuba
    Mar 14, 2017 at 6:39
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$
    – Kuba
    Mar 14, 2017 at 10:59


Browse other questions tagged or ask your own question.