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.)

  • 11
    $\begingroup$ What about \$InputFileName and \$Input? $\endgroup$
    – bobknight
    Jul 24, 2013 at 17:46
  • $\begingroup$ @bobknight, $InputFileName fits the bill. I'll gladly accept it if you post it as an Answer. $\endgroup$
    – kjo
    Jul 24, 2013 at 18:19

2 Answers 2


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.


$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.

  • $\begingroup$ For completeness: in version 7, $InputFileName is undocumented and located in System`Private` context. $\endgroup$ Jul 26, 2013 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[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"}]];

{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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.