MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to find out which directory a .m file is loaded from?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38936] Re: [mg38922] How to find out which directory a .m file is loaded from?
  • From: Todd Gayley <tgayley at wolfram.com>
  • Date: Tue, 21 Jan 2003 07:38:14 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

At 11:33 PM 1/18/03, John Harper wrote:
>I'd like, in a .m file for a package, to be able to know what directory
>the .m file is being loaded from, so I can search in the same place
>(or subdirectories) for related files. I've looked through everything
>to do with directories I can find in The Book, but I can't find anything
>that looks as though it would help.
>
>Anyone know how to do this? Thanks in advance...
>
>     John


John,

There is a nice trick for this. It works on every operating system and all 
versions of Mathematica from at least 4.0 onward (and probably earlier). It 
uses the undocumented function System`Private`FindFile, but if that ever 
goes away we'll have to add something to take its place because J/Link uses 
this exact technique to find its application resources.

Put a line like this in your .m file:

     thisPackageDirectory = DirectoryName[System`Private`FindFile[$Input]]

The variable thisPackageDirectory will get the directory name in which the 
.m file resides no matter how the user reads the package, e.g.:

     <<MyPackage.m
     <</full/path/to/MyPackage.m
     <<MyPackage`
     Needs["MyPackage`"]
     etc.

This trick also works if you use the standard Mathematica application 
directory structure (highly recommended), wherein you have a directory 
named for your package (say MyApp):

     <Mathematica dir>/
         Applications/
             MyApp/
                 MyApp.m
                 ... other files and dirs ...
                 Kernel/
                     init.m   <-- this file reads simply:  Get["MyApp`MyApp`"]

This standard application layout allows users to enter simply

     Needs["MyApp`"]

to load your MyApp.m file. The thisPackageDirectory variable would then get 
the value "<Mathematica dir>/Applications/MyApp".


Todd Gayley
Wolfram Research



  • Prev by Date: Re: How to find out which directory a .m file is loaded from?
  • Next by Date: Re: How to find out which directory a .m file is loaded from?
  • Previous by thread: Re: How to find out which directory a .m file is loaded from?
  • Next by thread: Re: System of coupled PDE's initial value problem