Thread overview | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 07, 2013 Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: cd ~/projects/program dmd Program.d -ofProgram That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? Thanks for any help! |
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alan | On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:
> Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example:
>
> cd ~/projects/program
> dmd Program.d -ofProgram
>
> That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory?
>
> Thanks for any help!
Is this what you are looking for?
find ~/projects/program/ -type f -perm +111
or
ls -d -1 ~/projects/program/*
|
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alan | On 08/06/2013 10:31 PM, Alan wrote:
> Hello! This may seem like a simple question, maybe an embarassing
> question but how would I fetch the absolute path to the directory where
> my executable is located? My wording is known to be confusing so I will
> give an example:
>
> cd ~/projects/program
> dmd Program.d -ofProgram
>
> That executable would be located in /home/alan/projects/program for
> example. SO my question is how would I fetch the absolute path to that
> directory?
>
> Thanks for any help!
First program argument is the absolute path to the executable. (I am not sure whether this is portable.)
import std.stdio;
import std.path;
void main(string[] args)
{
writefln("I am program '%s' in '%s'.",
baseName(args[0]), dirName(args[0]));
}
Ali
|
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Artus | On Wednesday, 7 August 2013 at 06:08:44 UTC, Andre Artus wrote:
> On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:
>> Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example:
>>
>> cd ~/projects/program
>> dmd Program.d -ofProgram
>>
>> That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory?
>>
>> Thanks for any help!
>
> Is this what you are looking for?
>
> find ~/projects/program/ -type f -perm +111
> or
> ls -d -1 ~/projects/program/*
No sorry, I meant how to find the directory in D code.
|
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alan | On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote: > Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example: > > cd ~/projects/program > dmd Program.d -ofProgram > > That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory? > > Thanks for any help! you mean absolute path at runtime? this is passed as args[0] in main(string[] args). though it contains not path but string used to launch ur program( for example "./program" when launched from its location, and full program path when double-clicked). also you can use getcwd() from std.path. but you can't rely on it as both of this takes "caller" location. may be this helps you : http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx http://man7.org/linux/man-pages/man3/getcwd.3.html p.s. such question better ask in learn section |
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 7 August 2013 at 06:10:16 UTC, Ali Çehreli wrote:
> On 08/06/2013 10:31 PM, Alan wrote:
>> Hello! This may seem like a simple question, maybe an embarassing
>> question but how would I fetch the absolute path to the directory where
>> my executable is located? My wording is known to be confusing so I will
>> give an example:
>>
>> cd ~/projects/program
>> dmd Program.d -ofProgram
>>
>> That executable would be located in /home/alan/projects/program for
>> example. SO my question is how would I fetch the absolute path to that
>> directory?
>>
>> Thanks for any help!
>
> First program argument is the absolute path to the executable. (I am not sure whether this is portable.)
>
> import std.stdio;
> import std.path;
>
> void main(string[] args)
> {
> writefln("I am program '%s' in '%s'.",
> baseName(args[0]), dirName(args[0]));
> }
>
> Ali
Just missed your response! And no that doesn't fetch the absolute path, just the relative path. Thanks for help so far though!
|
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to evilrat | On Wednesday, 7 August 2013 at 06:13:59 UTC, evilrat wrote:
> On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:
>> Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example:
>>
>> cd ~/projects/program
>> dmd Program.d -ofProgram
>>
>> That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory?
>>
>> Thanks for any help!
>
> you mean absolute path at runtime? this is passed as args[0] in main(string[] args). though it contains not path but string used to launch ur program( for example "./program" when launched from its location, and full program path when double-clicked).
>
> also you can use getcwd() from std.path.
>
> but you can't rely on it as both of this takes "caller" location.
>
> may be this helps you :
> http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx
> http://man7.org/linux/man-pages/man3/getcwd.3.html
>
> p.s. such question better ask in learn section
Sorry about the section mishap, I'm new here!
Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out.
Thanks for all the help so far!
|
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 7 August 2013 at 06:10:16 UTC, Ali Çehreli wrote:
>
> First program argument is the absolute path to the executable. (I am not sure whether this is portable.)
>
> import std.stdio;
> import std.path;
>
> void main(string[] args)
> {
> writefln("I am program '%s' in '%s'.",
> baseName(args[0]), dirName(args[0]));
> }
>
> Ali
the problem with this that its not absolute path, it is whatever string you started your app, i.e. lets assume ~/location/program is executable, and we call it
--
cd ~/location
cd ../
./location/prog
---
args[0] would contaion ./location/prog
but combine it with cwd and this would be (always?) the exact program path
|
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alan | On Wednesday, 7 August 2013 at 06:18:26 UTC, Alan wrote: > Believe me I have tried all sorts of combonations of this but it's not guarunteed accuracy in certain situations, I thought there would be a solution in phobos library I was missing but maybe I will have to write something complex out. > Thanks for all the help so far! putting all together try this(not tested) import std.path; void main(string[] args) { writeln(absolutePath(buildNormalizedPath(args[0]))); } |
August 07, 2013 Re: Finding the path to a executable? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alan | On Wednesday, 7 August 2013 at 06:10:35 UTC, Alan wrote:
> On Wednesday, 7 August 2013 at 06:08:44 UTC, Andre Artus wrote:
>> On Wednesday, 7 August 2013 at 05:31:24 UTC, Alan wrote:
>>> Hello! This may seem like a simple question, maybe an embarassing question but how would I fetch the absolute path to the directory where my executable is located? My wording is known to be confusing so I will give an example:
>>>
>>> cd ~/projects/program
>>> dmd Program.d -ofProgram
>>>
>>> That executable would be located in /home/alan/projects/program for example. SO my question is how would I fetch the absolute path to that directory?
>>>
>>> Thanks for any help!
>>
>> Is this what you are looking for?
>>
>> find ~/projects/program/ -type f -perm +111
>> or
>> ls -d -1 ~/projects/program/*
>
> No sorry, I meant how to find the directory in D code.
Sorry, I misunderstood.
|
Copyright © 1999-2021 by the D Language Foundation