Some additional details on $Input
. The Mathematica documentation states that $Input
is bound to the name of the stream from which the loading file is being read. But what is that stream name? It appears to be whatever string Get
, (<<
), or Needs
receive as their first argument.
To test this hypothesis, I made a .m file with the following contents and placed in on my system's desktop.
Print[Streams[]]
Print[Row[{"$Input: ", $Input}]]
Print[Row[{"$InputFileNamw: ", $InputFileName}]]
I then evaluated the following expressions from an open notebook:
Get[SystemDialogInput["FileOpen", "*.m"]]
{OutputStream[stdout, 1], OutputStream[stderr, 2], InputStream[/Users/oldmg/Desktop/TestPkg.m, 86]}
\$Input: /Users/oldmg/Desktop/TestPkg.m
\$InputFileName: /Users/oldmg/Desktop/TestPkg.m
SetDirectory[FileNameJoin[{HomeDirectory[], "Desktop"}]];
Get["TestPkg.m"]
{OutputStream[stdout, 1], OutputStream[stderr, 2], InputStream[TestPkg.m, 118]}
\$Input: TestPkg.m
\$InputFileName: /Users/oldmg/Desktop/TestPkg.m
Note in the first case, $Input
and $InputFileName
are bound to the same string. This is because SystemDialogInput
returns a full path name. In the second case, however, $Input
is bound to a string giving only the file name because that is all that was passed to it.
$InputFileName
fits the bill. I'll gladly accept it if you post it as an Answer. $\endgroup$ – kjo Jul 24 '13 at 18:19