full path to source file __FILE__

Jonathan Marler via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 22 06:50:55 PDT 2016


On Friday, 22 July 2016 at 13:30:10 UTC, Steven Schveighoffer 
wrote:
> On 7/22/16 3:47 AM, Jonathan Marler wrote:
>>> What's wrong with __FILE__.dirName ?
>>
>> It's kinda weird, sometimes I've noticed that the __FILE__ 
>> keyword is an
>> absolute path, and sometimes it isn't.
>
> If you combine it with current working directory, this should 
> give you the full path.
>
> Looks like std.path gives you a mechanism, I think this should 
> work:
>
> import std.path;
> auto p = __FILE__.absolutePath;
>
> http://dlang.org/phobos/std_path.html#.absolutePath
>
> -Steve

That doesn't work in the example I provided:

/somedir/clean.d
/somedir/build

Say clean.d is meant to remove the build directory that lives in 
the same path as the clean.d script itself.

shell/anypath> rdmd /somedir/clean.d
Removing /somedir/build...

Since you are running the script from "anypath", the information 
that clean.d exists at /somedir is lost.  The last component to 
know where the file was found is the compiler itself.


More information about the Digitalmars-d-learn mailing list