Re: How to find out which directory a .m file is loaded from?
- To: mathgroup at smc.vnet.net
- Subject: [mg38951] Re: [mg38922] How to find out which directory a .m file is loaded from?
- From: John Harper <jharper at cisco.com>
- Date: Tue, 21 Jan 2003 07:39:37 -0500 (EST)
- References: <200301190533.AAA18690@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Many thanks, this is exactly what I was hoping/looking for.
John
At 01:55 AM 1/20/2003 -0600, Todd Gayley wrote:
>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
>
- References:
- How to find out which directory a .m file is loaded from?
- From: "John Harper" <jharper@cisco.com>
- How to find out which directory a .m file is loaded from?