Re: What does NotebookDirectory[] do for .m files?
- To: mathgroup at smc.vnet.net
- Subject: [mg126078] Re: What does NotebookDirectory[] do for .m files?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sun, 15 Apr 2012 03:17:39 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jm8qeg$ja9$1@smc.vnet.net>
On 13/04/2012 10:08, Jesse Perla wrote: > NotebookDirectory[] correctly gives the location of the notebook for my .nb files, but I cannot seem to find an equivalent for my .m files? Is there another approach? > There are two distinct cases here. If you open a .m file in the FrontEnd, it is treated like a notebook, and NotebookDirectory will work on it. If you use Get ( << ) to open the file, or it is opened because of a Needs command, then things get a bit trickier. While the .m file is being loaded in, $InputFileName is set to the name of the .m file, but not to the complete path! This is extremely inconvenient, because it is usually the path that you want! However, there is another undocumented variable that holds the complete path: System`Private`$InputFileName This has remained unchanged over several versions of Mathematica, and it is the only fully reliable way that I know of, to obtain this information. For example, my SuperWidgetPackage contains many other files within its directory, which I access by saving this variable as the package is first executed. David Bailey http://www.dbaileyconsultancy.co.uk