Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9631 > unrolled thread

What does NotebookDirectory[] do for .m files?

Started byJesse Perla <jesseperla@gmail.com>
First post2012-04-13 09:08 +0000
Last post2012-04-18 07:55 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  What does NotebookDirectory[]  do for .m files? Jesse Perla <jesseperla@gmail.com> - 2012-04-13 09:08 +0000
    Re: What does NotebookDirectory[]  do for .m files? Vince Virgilio <blueschi@gmail.com> - 2012-04-14 07:16 +0000
    Re: What does NotebookDirectory[]  do for .m files? David Bailey <dave@removedbailey.co.uk> - 2012-04-15 07:18 +0000
      Re: What does NotebookDirectory[]  do for .m files? "Alexey Popkov" <lehin.p@gmail.com> - 2012-04-16 10:09 +0000
        Re: What does NotebookDirectory[]  do for .m files? Jesse Perla <jesseperla@gmail.com> - 2012-04-18 07:55 +0000

#9631 — What does NotebookDirectory[] do for .m files?

FromJesse Perla <jesseperla@gmail.com>
Date2012-04-13 09:08 +0000
SubjectWhat does NotebookDirectory[] do for .m files?
Message-ID<jm8qeg$ja9$1@smc.vnet.net>
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?

[toc] | [next] | [standalone]


#9646

FromVince Virgilio <blueschi@gmail.com>
Date2012-04-14 07:16 +0000
Message-ID<jmb88n$r6h$1@smc.vnet.net>
In reply to#9631
On Friday, April 13, 2012 5:08:32 AM UTC-4, 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?

In version 8, $InputFileName. In earlier versions there's nothing as reliable, but it can be approximated with FindFile[$Input].

Vince Virgilio

[toc] | [prev] | [next] | [standalone]


#9654

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-04-15 07:18 +0000
Message-ID<jmdsop$ac3$1@smc.vnet.net>
In reply to#9631
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

[toc] | [prev] | [next] | [standalone]


#9671

From"Alexey Popkov" <lehin.p@gmail.com>
Date2012-04-16 10:09 +0000
Message-ID<jmgr4e$pgr$1@smc.vnet.net>
In reply to#9654
"David Bailey" <dave@removedbailey.co.uk>: news:jmdsop$ac3$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!

It seems that in version 8.0.4 $InputFileName gives complete path to the 
.m-file. Evaluating

<< ExampleData/input.m

gives

$Input is ExampleData/input.m
$InputFileName is C:\Program Files\Wolfram 
Research\Mathematica\8.0\Documentation\English\System\ExampleData\input.m

while evaluating

FilePrint["ExampleData/input.m"]

gives

Print["$Input is ",$Input];
Print["$InputFileName is ",$InputFileName];


[toc] | [prev] | [next] | [standalone]


#9683

FromJesse Perla <jesseperla@gmail.com>
Date2012-04-18 07:55 +0000
Message-ID<jmls2a$jel$1@smc.vnet.net>
In reply to#9671
On Monday, April 16, 2012 6:09:18 AM UTC-4, Alexey Popkov wrote:
> "David Bailey"
> > On 13/04/2012 10:08, Jesse Perla wrote:
> > 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.

Thanks, I understand my confusion now.

> > 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!

> Print["$Input is ",$Input];
> Print["$InputFileName is ",$InputFileName];
What if I am running command-line mathematica?  I tried "math.exe < myfile.m" with all three of those variables, to no avail.


To take a more direct approach, what I really want is the ability save output relative to where I run my Mathematica file from (in both the Windows FrontEnd for debugging and command-line linux when I run on a cluster).

It looks like:
* "Directory[]" is set to the current system path when run from the command line.  So if I was careful to run the command-line from the directory of the current .m file, it should work
* "Directory[]" is set to some system default when I open a .m file in the front-end and run it.

So here is a proposed solution, which works on my Windows machine at least:

notebookDirectoryExists = Check[NotebookDirectory[],0]
outputDirectory = If[notebookDirectoryExists == 0, Directory[], notebookDirectoryExists]
Print["outputDirectory is ",outputDirectory]; 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web