# How to extract the drive letter and treat it as a variable, or alternatively specify a relative path?

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.

• Do you know about $InstallationDirectory and NotebookDirectory[]? – Thies Heidecke Aug 23 '17 at 16:32 • No I didn't. Thanks for this. – user51827 Aug 23 '17 at 16:56 ## 1 Answer 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

• 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. – user51827 Aug 23 '17 at 17:00