0
$\begingroup$

I am a little confused because I belief that the behaviour of SetDirectory was different before. The intention is to be able to open notebooks in the same folder only by specifying the file name, starting from "Main.nb":

(* sets current dir *)
SetDirectory[NotebookDirectory[]]

and, for checking purposes:

(* check 1... *)
Directory[]

(* yields *)
C:\Users\armin\Documents\Temp\Test

and

(* check 2... *)
FileNames[]

(* yields *)
{Main.nb, Sub.nb}

But then, unfortunately:

In[26]:= (* however... *)
NotebookOpen["Sub.nb"]

Out[26]= $Failed

I was hoping, that the initial SetDirectory[NotebookDirectory[]] would be sufficient. That is working, of course:

In[27]:= (* instead, this works *)
NotebookOpen[FileNameJoin[{NotebookDirectory[], "Sub.nb"}]]

Out[27]= NotebookObject["bunch-of-characters..."]

I know, there is a similar question, but with slightly different subject: SetDirectory[NotebookDirectory[]] in one notebook causes Directory[] to change in a different notebook

Any comments?

$\endgroup$

1 Answer 1

1
$\begingroup$

As mentioned in the documentation for NotebookOpen under Possible Issues:

A relative file name locates files relative to the front end's NotebookPath setting.

Furthermore, the documentation clearly states that

NotebookOpen does not look in the current working directory for the specified file name.

Therefore, you should manually provide the full path:

NotebookOpen[FileNameJoin[{Directory[], "Sub.nb"}]];

You can see which paths are included by default with Options[$FrontEnd, NotebookPath]. To always include also the current directory, use:

CurrentValue[$FrontEnd, NotebookPath] = 
 AppendTo[CurrentValue[$FrontEnd, NotebookPath], 
  FrontEnd`FileName[Directory[]]];
$\endgroup$
2
  • $\begingroup$ Thanks! I've marked this as a solution, but the behaviour in question is, I'm sorry to say, quite counterintuitive. $\endgroup$ Mar 4 at 20:04
  • $\begingroup$ @ArminVollmer, I don't know about the reasoning behind this design decision. However, I have added a permanent solution to my answer should you need one. $\endgroup$
    – Domen
    Mar 5 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.