Re: What does NotebookDirectory[] do for .m files?
- To: mathgroup at smc.vnet.net
- Subject: [mg126110] Re: What does NotebookDirectory[] do for .m files?
- From: Jesse Perla <jesseperla at gmail.com>
- Date: Wed, 18 Apr 2012 03:54:13 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jm8qeg$ja9$1@smc.vnet.net> <jmdsop$ac3$1@smc.vnet.net> <jmgr4e$pgr$1@smc.vnet.net>
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];