digitalmars.D.learn - full path to source file __FILE__
- Jonathan Marler (17/17) Jul 21 2016 Is there a way to get the full path of the current source file?
- zabruk70 (9/10) Jul 21 2016 won't? what this means?
- Adam D. Ruppe (4/5) Jul 21 2016 That gives the path to the .exe but he wants the path to the .d.
- Jonathan Marler (2/7) Jul 21 2016 I explain in the original post. Any ideas Adam? Thanks in advance.
- Adam D. Ruppe (4/6) Jul 21 2016 But why does the batch script use it? Since you are rewriting
- Jonathan Marler (4/11) Jul 21 2016 The script depends on other files relative to where it exists on
- Jacob Carlborg (9/12) Jul 21 2016 What kind of files are we talking about. Resource files, config files?
- Jonathan Marler (18/30) Jul 22 2016 I suppose I should have been more specific. The script actually
- Kagamin (5/11) Jul 22 2016 Don't just ignore Adam's question :)
- Adam D. Ruppe (8/9) Jul 22 2016 eh he answered it.
- Steven Schveighoffer (6/23) Jul 21 2016 Sure seems like an unwanted limitation.
- Jonathan Marler (5/37) Jul 21 2016 An option for rdmd would be good, but then requires the user to
- Jonathan M Davis via Digitalmars-d-learn (15/44) Jul 21 2016 It would be pretty terrible actually to put the executable in the source
- Jonathan Marler (6/24) Jul 21 2016 I agree this isn't a very good solution for the problem at hand.
- Jonathan M Davis via Digitalmars-d-learn (6/23) Jul 21 2016 Well, while it might not be what you want, the obvious solution is to ju...
- fdgdsgf (2/20) Jul 21 2016 What's wrong with __FILE__.dirName ?
- Jonathan Marler (7/33) Jul 22 2016 It's kinda weird, sometimes I've noticed that the __FILE__
- sdhdfhed (7/15) Jul 22 2016 Personally I've never seen a relative __FILE__. Is this an issue
- sdhdfhed (4/12) Jul 22 2016 make a PR that expands the sources passed to the dmd to their
- Jonathan Marler (8/26) Jul 22 2016 It's definitely confirmed. And now that I've walked through the
- sdhdfhed (7/36) Jul 22 2016 Yes, i've seen he 's started to review.
- Jonathan Marler (8/46) Jul 22 2016 Again that's Walter's call. The __FILE__ trait seems to be used
- sdhdfhed (18/23) Jul 22 2016 Another usage is for testing parsers or string functions directly
- Jonathan Marler (6/29) Jul 22 2016 Actually I realized if __FILE__ was always absolute, then all
- Jonathan M Davis via Digitalmars-d-learn (17/21) Jul 22 2016 In some cases, it could also be viewed as a security risk. For instance,...
- Steven Schveighoffer (8/11) Jul 22 2016 If you combine it with current working directory, this should give you
- Jonathan Marler (12/26) Jul 22 2016 That doesn't work in the example I provided:
- Kagamin (5/7) Jul 22 2016 So for command rdmd /somedir/clean.d what __FILE__ contains? LDC
- Steven Schveighoffer (7/14) Jul 22 2016 The issue which is not being expressed completely by Jonathan, is that
- Jonathan Marler (3/21) Jul 22 2016 Thanks for pointing this out, somehow I overlooked this use case.
- Martin Tschierschke (10/22) Jul 23 2016 What about using a wrapper around rdmd, so the program is
- Jonathan Marler (7/25) Jul 27 2016 For others who may see this thread, the __FULL_FILE_PATH__
- DigitalDesigns (7/38) May 29 2018 Except it doesn't?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/10) May 29 2018 __FILE_FULL_PATH__
- DigitalDesigns (2/12) May 29 2018 Lol, thanks:
- Steven Schveighoffer (9/49) May 29 2018 __FILE_FULL_PATH__, not __FULL_FILE_PATH__ (yes, it was wrong in the
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 ", ???); }
Jul 21 2016
On Thursday, 21 July 2016 at 19:54:34 UTC, Jonathan Marler wrote:thisExePath won't work.won't? what this means? this work on my windows import std.file: thisExePath; import std.stdio: writeln; void main() { writeln(thisExePath()); }
Jul 21 2016
On Thursday, 21 July 2016 at 22:28:39 UTC, zabruk70 wrote:won't? what this means?That gives the path to the .exe but he wants the path to the .d. But why? I would think the current working directory is probably adequate and that's easy to get...
Jul 21 2016
On Thursday, 21 July 2016 at 22:33:39 UTC, Adam D. Ruppe wrote:On Thursday, 21 July 2016 at 22:28:39 UTC, zabruk70 wrote:I explain in the original post. Any ideas Adam? Thanks in advance.won't? what this means?That gives the path to the .exe but he wants the path to the .d. But why? I would think the current working directory is probably adequate and that's easy to get...
Jul 21 2016
On Thursday, 21 July 2016 at 22:47:42 UTC, Jonathan Marler wrote:I explain in the original post. Any ideas Adam? Thanks in advance.But why does the batch script use it? Since you are rewriting anyway, maybe you can find an easier/better way to achieve the goal.
Jul 21 2016
On Friday, 22 July 2016 at 01:52:57 UTC, Adam D. Ruppe wrote:On Thursday, 21 July 2016 at 22:47:42 UTC, Jonathan Marler wrote:The script depends on other files relative to where it exists on the file system. I couldn't think of a better design to find these files then knowing where the script exists, can you?I explain in the original post. Any ideas Adam? Thanks in advance.But why does the batch script use it? Since you are rewriting anyway, maybe you can find an easier/better way to achieve the goal.
Jul 21 2016
On 2016-07-22 04:24, Jonathan Marler wrote:The script depends on other files relative to where it exists on the file system. I couldn't think of a better design to find these files then knowing where the script exists, can you?What kind of files are we talking about. Resource files, config files? Are they static? For static resource files you can bundle them in the executable with a string import. For config files it might be better to store it in a completely different directory, like the user's home directory. This actually depends on what kind of config files and the operating system. -- /Jacob Carlborg
Jul 21 2016
On Friday, 22 July 2016 at 06:45:58 UTC, Jacob Carlborg wrote:On 2016-07-22 04:24, Jonathan Marler wrote:I suppose I should have been more specific. The script actually operates on the filesystem relative to where it lives. It copies files, modifies directories, etc. It is meant to be ran from any directory, but is only meant to modify the filesystem relative to where it lives. Take a simple example of a clean script: /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... It's important to remember that the clean.d script is ran with rdmd, and that it is meant to be called from any directory. Since it's ran with rdmd, the thisExePath won't give you the right directory, and since you can call it from any directory, you also can't use the current directory. As you can see, what you really want to know is where the script itself lives.The script depends on other files relative to where it exists on the file system. I couldn't think of a better design to find these files then knowing where the script exists, can you?What kind of files are we talking about. Resource files, config files? Are they static? For static resource files you can bundle them in the executable with a string import. For config files it might be better to store it in a completely different directory, like the user's home directory. This actually depends on what kind of config files and the operating system.
Jul 22 2016
On Friday, 22 July 2016 at 07:53:17 UTC, Jonathan Marler wrote:It's important to remember that the clean.d script is ran with rdmd, and that it is meant to be called from any directory. Since it's ran with rdmd, the thisExePath won't give you the right directory, and since you can call it from any directory, you also can't use the current directory. As you can see, what you really want to know is where the script itself lives.Don't just ignore Adam's question :) https://dlang.org/phobos/std_path.html#.absolutePath https://dlang.org/phobos/std_file.html#.getcwd - why this won't work for you?
Jul 22 2016
On Friday, 22 July 2016 at 10:51:57 UTC, Kagamin wrote:Don't just ignore Adam's question :)eh he answered it. On Windows, it is somewhat common for things to be loaded or modified (especially on older versions when these were still writable...) from the program's directory. Its support files are all put together with the exe. He's trying to make a "script" that is run with rdmd that pretends to work just like an exe and works with files around it.
Jul 22 2016
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. -Steve
Jul 21 2016
On Thursday, 21 July 2016 at 22:39:45 UTC, Steven Schveighoffer wrote:On 7/21/16 3:54 PM, Jonathan Marler wrote:An option for rdmd would be good, but then requires the user to call rdmd in a particular way. It doesnt allow the script itself know where it lives, which is needed in my case.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. -Steve
Jul 21 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: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 DavisIs 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.
Jul 21 2016
On Thursday, 21 July 2016 at 22:57:06 UTC, Jonathan M Davis wrote:On Thursday, July 21, 2016 18:39:45 Steven Schveighoffer via Digitalmars-d- learn wrote:I agree this isn't a very good solution for the problem at hand. Putting the executable in a temporary directory makes sense in any cases I can think of. I posted an idea for another potential solution (http://forum.dlang.org/thread/cmydxneeghtjqjroxpld forum.dlang.org), please let me know your thoughts. Thanks.[...]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
Jul 21 2016
On Thursday, July 21, 2016 19:54:34 Jonathan Marler via Digitalmars-d-learn 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 ", ???); }Well, while it might not be what you want, the obvious solution is to just compile it as an executable and put that where you want rather than making it a script. - Jonathan M Davis
Jul 21 2016
On Thursday, 21 July 2016 at 19:54:34 UTC, 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 ", ???); }What's wrong with __FILE__.dirName ?
Jul 21 2016
On Friday, 22 July 2016 at 05:41:00 UTC, fdgdsgf wrote:On Thursday, 21 July 2016 at 19:54:34 UTC, Jonathan Marler wrote:It's kinda weird, sometimes I've noticed that the __FILE__ keyword is an absolute path, and sometimes it isn't. If it was always an absolute path, that would work. I decided to take a stab at implementing this in the dmd compiler: https://github.com/dlang/dmd/pull/5959 It adds a __FILE_FULL_PATH__ trait which would solve the issue.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 ", ???); }What's wrong with __FILE__.dirName ?
Jul 22 2016
On Friday, 22 July 2016 at 07:47:14 UTC, Jonathan Marler wrote:On Friday, 22 July 2016 at 05:41:00 UTC, fdgdsgf wrote:Personally I've never seen a relative __FILE__. Is this an issue that's confirmed ? I mean that it would be better to fix __FILE__ so that its result is always absolute then. I think that such a "PPR" (punk-pull-request) has 0% chance of being accepted, especially since it adds a special keyword !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 it was always an absolute path, that would work. I decided to take a stab at implementing this in the dmd compiler: https://github.com/dlang/dmd/pull/5959 It adds a __FILE_FULL_PATH__ trait which would solve the issue.
Jul 22 2016
On Friday, 22 July 2016 at 07:57:35 UTC, sdhdfhed wrote:On Friday, 22 July 2016 at 07:47:14 UTC, Jonathan Marler wrote:make a PR that expands the sources passed to the dmd to their absolute name. This is more likely to fix your issue and to be accepted.[...]Personally I've never seen a relative __FILE__. Is this an issue that's confirmed ? I mean that it would be better to fix __FILE__ so that its result is always absolute then. I think that such a "PPR" (punk-pull-request) has 0% chance of being accepted, especially since it adds a special keyword !
Jul 22 2016
On Friday, 22 July 2016 at 07:57:35 UTC, sdhdfhed wrote:On Friday, 22 July 2016 at 07:47:14 UTC, Jonathan Marler wrote:It's definitely confirmed. And now that I've walked through the source code, I see that it wasn't implemented to be an absolute path, it just happens to be some of the time depending on how the file is found. I'm sure Walter will have an opinion as to what solution he prefers. Either redefining the __FILE__ trait or adding a new one. He's communicating fixes to the PR on github so that a good sign. We'll see.On Friday, 22 July 2016 at 05:41:00 UTC, fdgdsgf wrote:Personally I've never seen a relative __FILE__. Is this an issue that's confirmed ? I mean that it would be better to fix __FILE__ so that its result is always absolute then. I think that such a "PPR" (punk-pull-request) has 0% chance of being accepted, especially since it adds a special keyword !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 it was always an absolute path, that would work. I decided to take a stab at implementing this in the dmd compiler: https://github.com/dlang/dmd/pull/5959 It adds a __FILE_FULL_PATH__ trait which would solve the issue.
Jul 22 2016
On Friday, 22 July 2016 at 08:36:37 UTC, Jonathan Marler wrote:On Friday, 22 July 2016 at 07:57:35 UTC, sdhdfhed wrote:Yes, i've seen he 's started to review. I don't know if you've seen my other suggestion but another solution would be to force relative fnames passed to the compiler to be translated to absolute. This is also why I've never seen a relative __FILE__. The build tool I use always does the expansion in intern before calling the compiler.On Friday, 22 July 2016 at 07:47:14 UTC, Jonathan Marler wrote:It's definitely confirmed. And now that I've walked through the source code, I see that it wasn't implemented to be an absolute path, it just happens to be some of the time depending on how the file is found. I'm sure Walter will have an opinion as to what solution he prefers. Either redefining the __FILE__ trait or adding a new one. He's communicating fixes to the PR on github so that a good sign. We'll see.On Friday, 22 July 2016 at 05:41:00 UTC, fdgdsgf wrote:Personally I've never seen a relative __FILE__. Is this an issue that's confirmed ? I mean that it would be better to fix __FILE__ so that its result is always absolute then. I think that such a "PPR" (punk-pull-request) has 0% chance of being accepted, especially since it adds a special keyword !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 it was always an absolute path, that would work. I decided to take a stab at implementing this in the dmd compiler: https://github.com/dlang/dmd/pull/5959 It adds a __FILE_FULL_PATH__ trait which would solve the issue.
Jul 22 2016
On Friday, 22 July 2016 at 09:37:24 UTC, sdhdfhed wrote:On Friday, 22 July 2016 at 08:36:37 UTC, Jonathan Marler wrote:Again that's Walter's call. The __FILE__ trait seems to be used most useful for error messages. I could see him wanting it to be a relative path sometimes and an absolute one other times. By redefining it to always be absolute would solve this problem, but might make others things harder. I'm not particularly for or against either solution (not sure why you're trying to convince me of this one), that would be up to the owners of the language :)On Friday, 22 July 2016 at 07:57:35 UTC, sdhdfhed wrote:Yes, i've seen he 's started to review. I don't know if you've seen my other suggestion but another solution would be to force relative fnames passed to the compiler to be translated to absolute. This is also why I've never seen a relative __FILE__. The build tool I use always does the expansion in intern before calling the compiler.On Friday, 22 July 2016 at 07:47:14 UTC, Jonathan Marler wrote:It's definitely confirmed. And now that I've walked through the source code, I see that it wasn't implemented to be an absolute path, it just happens to be some of the time depending on how the file is found. I'm sure Walter will have an opinion as to what solution he prefers. Either redefining the __FILE__ trait or adding a new one. He's communicating fixes to the PR on github so that a good sign. We'll see.On Friday, 22 July 2016 at 05:41:00 UTC, fdgdsgf wrote:Personally I've never seen a relative __FILE__. Is this an issue that's confirmed ? I mean that it would be better to fix __FILE__ so that its result is always absolute then. I think that such a "PPR" (punk-pull-request) has 0% chance of being accepted, especially since it adds a special keyword !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 it was always an absolute path, that would work. I decided to take a stab at implementing this in the dmd compiler: https://github.com/dlang/dmd/pull/5959 It adds a __FILE_FULL_PATH__ trait which would solve the issue.
Jul 22 2016
On Friday, 22 July 2016 at 14:02:03 UTC, Jonathan Marler wrote:The __FILE__ trait seems to be used most useful for error messages.Another usage is for testing parsers or string functions directly on the source. E.g in "devel" mode the main function void main(string[] args) { version(devel) { // dont mess with params, use the text in source to catch most simple bugs. File f = File(__FILE__, "r"); } else { // load using args } }I could see him wanting it to be a relative path sometimes and an absolute one other times. By redefining it to always be absolute would solve this problem,I'm for this, always absolute. Eventually forced by a new switch: default behavior is not changed.
Jul 22 2016
On Friday, 22 July 2016 at 19:13:31 UTC, sdhdfhed wrote:On Friday, 22 July 2016 at 14:02:03 UTC, Jonathan Marler wrote:Actually I realized if __FILE__ was always absolute, then all your exception messages would contain the full path of the file it was thrown from on the machine it was compiled on. This would be quite odd. Both a relative and absolute version are useful in different cases.The __FILE__ trait seems to be used most useful for error messages.Another usage is for testing parsers or string functions directly on the source. E.g in "devel" mode the main function void main(string[] args) { version(devel) { // dont mess with params, use the text in source to catch most simple bugs. File f = File(__FILE__, "r"); } else { // load using args } }I could see him wanting it to be a relative path sometimes and an absolute one other times. By redefining it to always be absolute would solve this problem,I'm for this, always absolute. Eventually forced by a new switch: default behavior is not changed.
Jul 22 2016
On Friday, July 22, 2016 19:28:05 Jonathan Marler via Digitalmars-d-learn wrote:Actually I realized if __FILE__ was always absolute, then all your exception messages would contain the full path of the file it was thrown from on the machine it was compiled on. This would be quite odd.In some cases, it could also be viewed as a security risk. For instance, on *nix systems, it would almost certainly give away the username of the user that built it. Also, it would result in needlessly long error messages when exceptions were thrown, which could impact performance as well as making log files that much more annoying. I'm definietly inclined to think that making __FILE__ absolute would be a mistake. Maybe something else like __FILE_ABSOLUTE__ would be okay, but in this particular case, I'd argue that you should just not make it a script if you need additional files that are next to it rather than in a known place. And as far as your example of build files goes, it's normal to have to run stuff like that in the directory where it lives (e.g. that's what happens with make), so while I understand that it may be annoying, I don't think that it's a compelling use case for changing what __FILE__ does. - Jonathan M Davis
Jul 22 2016
On 7/22/16 3:47 AM, Jonathan Marler wrote: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; -SteveWhat'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.
Jul 22 2016
On Friday, 22 July 2016 at 13:30:10 UTC, Steven Schveighoffer wrote:On 7/22/16 3:47 AM, Jonathan Marler wrote: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.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; -SteveWhat'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.
Jul 22 2016
On Friday, 22 July 2016 at 13:50:55 UTC, Jonathan Marler wrote:shell/anypath> rdmd /somedir/clean.d Removing /somedir/build...So for command rdmd /somedir/clean.d what __FILE__ contains? LDC tells me the same path as specified on the command line, and that is specified relative to current directory, where the compiler is called, so absolutePath(__FILE__) should give the right result.
Jul 22 2016
On 7/22/16 2:43 PM, Kagamin wrote:On Friday, 22 July 2016 at 13:50:55 UTC, Jonathan Marler wrote:The issue which is not being expressed completely by Jonathan, is that rdmd caches the build. So if I run the script from one directory, then cd elsewhere, it has the same __FILE__ as before, but the cwd has moved. So it won't work. I had assumed rdmd would rebuild, but it doesn't. -Steveshell/anypath> rdmd /somedir/clean.d Removing /somedir/build...So for command rdmd /somedir/clean.d what __FILE__ contains? LDC tells me the same path as specified on the command line, and that is specified relative to current directory, where the compiler is called, so absolutePath(__FILE__) should give the right result.
Jul 22 2016
On Friday, 22 July 2016 at 19:23:30 UTC, Steven Schveighoffer wrote:On 7/22/16 2:43 PM, Kagamin wrote:Thanks for pointing this out, somehow I overlooked this use case.On Friday, 22 July 2016 at 13:50:55 UTC, Jonathan Marler wrote:The issue which is not being expressed completely by Jonathan, is that rdmd caches the build. So if I run the script from one directory, then cd elsewhere, it has the same __FILE__ as before, but the cwd has moved. So it won't work. I had assumed rdmd would rebuild, but it doesn't. -Steveshell/anypath> rdmd /somedir/clean.d Removing /somedir/build...So for command rdmd /somedir/clean.d what __FILE__ contains? LDC tells me the same path as specified on the command line, and that is specified relative to current directory, where the compiler is called, so absolutePath(__FILE__) should give the right result.
Jul 22 2016
On Thursday, 21 July 2016 at 19:54:34 UTC, 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 " %~dp0What about using a wrapper around rdmd, so the program is executed with an additional parameter or an environment variable containing "FULL_PATH". When I started with D, I "accidentaly" wrote my own replacement script for rdmd compiling xyz.d with dmd to xyz or calling xyz depending on the modification times of the files. Regards mt.
Jul 23 2016
On Thursday, 21 July 2016 at 19:54:34 UTC, 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 ", ???); }For others who may see this thread, the __FULL_FILE_PATH__ special trait was added to the dmd compiler with this PR: https://github.com/dlang/dmd/pull/5959 At the time of this post, the latest released version of D is 2.071.1, so this trait should be available on any release after that.
Jul 27 2016
On Wednesday, 27 July 2016 at 13:58:22 UTC, Jonathan Marler wrote:On Thursday, 21 July 2016 at 19:54:34 UTC, Jonathan Marler wrote:Except it doesn't? auto x(string fp = __FULL_FILE_PATH__)() { pragma(msg, fp); } ?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 ", ???); }For others who may see this thread, the __FULL_FILE_PATH__ special trait was added to the dmd compiler with this PR: https://github.com/dlang/dmd/pull/5959 At the time of this post, the latest released version of D is 2.071.1, so this trait should be available on any release after that.
May 29 2018
On 05/29/2018 02:34 PM, DigitalDesigns wrote: > auto x(string fp = __FULL_FILE_PATH__)(){ pragma(msg, fp); } ?__FILE_FULL_PATH__ https://dlang.org/spec/expression#specialkeywords Ali
May 29 2018
On Tuesday, 29 May 2018 at 21:41:37 UTC, Ali Çehreli wrote:On 05/29/2018 02:34 PM, DigitalDesigns wrote: > auto x(string fp = __FULL_FILE_PATH__)()Lol, thanks:{ pragma(msg, fp); } ?__FILE_FULL_PATH__ https://dlang.org/spec/expression#specialkeywords Ali
May 29 2018
On 5/29/18 5:34 PM, DigitalDesigns wrote:On Wednesday, 27 July 2016 at 13:58:22 UTC, Jonathan Marler wrote:__FILE_FULL_PATH__, not __FULL_FILE_PATH__ (yes, it was wrong in the post you quoted). In addition, you must instantiate the template: void main() { x(); } -SteveOn Thursday, 21 July 2016 at 19:54:34 UTC, Jonathan Marler wrote:Except it doesn't? auto x(string fp = __FULL_FILE_PATH__)() { pragma(msg, fp); } ?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 ", ???); }For others who may see this thread, the __FULL_FILE_PATH__ special trait was added to the dmd compiler with this PR: https://github.com/dlang/dmd/pull/5959 At the time of this post, the latest released version of D is 2.071.1, so this trait should be available on any release after that.
May 29 2018