# Equivalent of __FILE__?

Many languages define a special identifier whose value is the path to the source file currently being processed. (For example, in C, this is done through the __FILE__ preprocessor macro.)

Does Mathematica have anything like this (at least for *.m files)?

(I could not find a tag that adequately represents the topic of this question (namely, something like "source file processing"); suggestions welcome.)

-
What about \$InputFileName and \$Input? –  bobknight Jul 24 '13 at 17:46
@bobknight, $InputFileName fits the bill. I'll gladly accept it if you post it as an Answer. – kjo Jul 24 '13 at 18:19 add comment ## 2 Answers Just to get an answer on record. As bobknight stated in the comments $InputFileName is bound to a string giving the full path name of the file being read when code loading operations initiated by Get, (<<), or Needs are in the process of loading. The file being loaded can make use this information, but the notebook requesting the load cannot (think of it as if $InputFileName were bound to the path-name inside a Block, which may actually be the case). $Input is a system variable that works similarly to $InputFileName but only gives the file name. ### Correction $Inputis a system variable that works similarly to $InputFileName, but is bound to name of the stream from which the loading file is being read. See my additional answer for more detail. - For completeness: in version 7, $InputFileName is undocumented and located in SystemPrivate context. –  Oleksandr R. Jul 26 '13 at 18:04

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.

-