Say that on my hard drive I have got the Mathematica program and also a directory that contains a subdirectory of text and data files and another subdirectory of Mathematica notebooks. I use the notebooks to process the text and data files.

Then say I duplicate all of the above on a flash drive, which I use when working on other computers, including in public libraries. I do the syncing by hand.

In the notebooks, I specify the paths of the text and data files by beginning with the drive letter. On my home system, my hard drive is C. On a library system, my flash drive might be called anything, perhaps H. Problem: how to avoid having different copies of the notebooks for the hard drive and the flash drive, with paths specified as beginning C on one and H on the other.

Is there a way when running Mathematica to extract the letter that the OS assigns to the drive that Mathematica itself is installed on?

Alternatively, can I specify file paths relatively and if so, how?

I'd be interested in both ways of doing it.

  • 4
    $\begingroup$ Do you know about $InstallationDirectory and NotebookDirectory[]? $\endgroup$ Aug 23, 2017 at 16:32
  • $\begingroup$ No I didn't. Thanks for this. $\endgroup$
    – user51827
    Aug 23, 2017 at 16:56

1 Answer 1


You can get the name of the drive where Mathematica is installed (In windows) using FileNameTakeand $InstallationDirectory

FileNameTake[$InstallationDirectory, 1]
(* "C:\\" *)

The advice by Thies Heidecke is also a good idea, just keep all the references relatives to the NotebookDirectory.

FileNameJoin[{NotebookDirectory[], "File.name"}]

Code tested in Mathemathica 11.1.1 on Win7

  • 1
    $\begingroup$ Excellent. Many thanks! I've now also found StringTake[Directory[],1], which is uglier and grabs just the "C" (I'm unlikely to be on a system with more than 26 drives), but the relative path method seems more straightforward. It is so impressive that Mathematica makes these things so easy. $\endgroup$
    – user51827
    Aug 23, 2017 at 17:00

Your Answer

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