full path to source file __FILE__

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 21 15:57:06 PDT 2016


On Thursday, July 21, 2016 18:39:45 Steven Schveighoffer via Digitalmars-d-
learn wrote:
> On 7/21/16 3:54 PM, Jonathan Marler wrote:
> > Is there a way to get the full path of the current source file?
> > Something like:
> >
> > __FILE_FULL_PATH__
> >
> > I'm asking because I'm rewriting a batch script in D, meant to be ran
> > with rdmd.  However, the script needs to know it's own path.  The
> > original batch script uses the %~dp0 variable for this, but I'm at a
> > loss on how to do this in D.  Since rdmd compiles the executable to the
> > %TEMP% directory, thisExePath won't work.
> >
> > BATCH
> > -----
> > echo "Directory of this script is " %~dp0
> >
> >
> > DLANG
> > -----
> > import std.stdio;
> > int main(string[] args) {
> >
> >     writeln("Directory of this script is ", ???);
> >
> > }
>
> Sure seems like an unwanted limitation.
>
> rdmd does forward all dmd options, but there isn't really an option to
> say "put the exe in the source path".
>
> You should file an enhancement.

It would be pretty terrible actually to put the executable in the source
path, and in many cases, the user wouldn't even have the permissions for it.
For instance, what if the script were in /usr/local/bin? They won't have the
permissions for the executable to end up there, and it would just cause a
bunch of clutter in /usr/local/bin, since you'd get a new executable every
time it decided that it needed to rebuild it (and you wouldn't want it to
delete the executable every time, otherwise it would have to rebuild it
every time, making it so that it would _always_ have to compile your script
when it runs instead of just sometimes). Right now, the executable ends up
in a temp directory, which makes a lot of sense.

Maybe it would make sense to have such a flag for very rare cases, but in
general, it seems like a terrible idea.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list