# Relative paths for portable notebooks in Mathematica

I cannot find how to use relative paths in Mathematica. My directory structure is simple.

Import["G:\\Research\\Acc and Vel Runs\\5-24\\Mathematica\\Data\\250 \
Acc.xls"][[1]] // TableForm

That demonstrates the absolute path by using the insert path from the menus. I want this notebook to be portable. I want to give someone the "Mathematica" directory and I want them to be able to run the code. I don't want the paths to break because It will be run on a different machine. Basically I just want to use a relative path starting at the Mathematica level shown above.

-

If your notebook is elsewhere in the directory tree and you want to set paths relative to a different directory, then you could define a global $ParentDirectory and then use all paths relative to that by joining strings as in the above example. Then all that the other person needs to do is to set this global value once and they're good. For example:$ParentDirectory = FileNameJoin[{"absolute", "path", "to", "mathematica"}];
Import[FileNameJoin[{$ParentDirectory, "path", "to", "your", "file.xls"}] As Albert Retey points out, you can also use Directory[NotebookDirectory[]] to give you the parent directory of the notebook's directory. In other words, it is an equivalent of cd .. from that directory and can be nested as many times as required. - This is exactly what I wanted. But your second suggestion with the "ParentDirectory" is interesting but I don't quite understand it. Could you possibly provide a little more explanation? Isn't the "ParentDirectory" an absolute path? PS I will accept your answer as soon as the timer allows – Matthew Kemnetz May 24 '12 at 23:04 It is an absolute path. The way you use it is you set$ParentDirectory once and use relative paths from there onwards. When you give it to someone, they set that variable once at the beginning of the notebook/session (or in an init.m file) to what they want it to be and it all works fine. The usage is similar to how Mathematica uses $UserBaseDirectory,$InstallationDirectory, etc. for its stuff. It's hardcoded somewhere, but you can easily change it. This is only in the case where you want to use a different directory than NotebookDirectory[]. If that works, then it's the simplest. – rm -rf May 24 '12 at 23:12