Consider a situation when we import some file in a .nb from a sub-directory located at the directory where .nb is located. We need to write specific path to the file. Next, suppose that we moved file to another directory. Then one needs to change the directory of the the file in .nb.

Is there any way to allow Mathematica to import the file simply by its name, without the specification of its location? I.e., to set up the file name and that the file is located somewhere in the .nb directory.

  • 1
    $\begingroup$ Have you tried using NotebookDirectory[] to refer to the the directory the notebook is currently in? You can use FileNameJoin to build up paths from there. $\endgroup$ – Sjoerd Smit Sep 12 '18 at 11:03
  • 1
    $\begingroup$ @SjoerdSmit : I move not the notebook, but the file which I want to import from sub-directory A to sub-directory B. And I don't want each time to change the sub-directory in SetDirectory[]; $\endgroup$ – John Taylor Sep 12 '18 at 11:19
  • 2
    $\begingroup$ In that case, I recommend you look up the documentation for FindFile. $\endgroup$ – Sjoerd Smit Sep 12 '18 at 11:21
  • 2
    $\begingroup$ ParentDirectory together with NotebookDirectory might be helpful. $\endgroup$ – Αλέξανδρος Ζεγγ Sep 12 '18 at 12:28
  • 2
    $\begingroup$ Does FileNames["file.ext", NotebookDirectory[], Infinity] do what you're after? $\endgroup$ – Lukas Lang Sep 12 '18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.