www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - running a command in a directory using std.process

reply Benjamin Thaut <code benjamin-thaut.de> writes:
As far as I can tell std.process can only run commands in the working 
directory of the currently executing function. I want to execute a 
certain program inside a subdirectory on windows and can't get it to work:

myproject
  |- subdir

So my executable has "myproject" as working directory. And I want to 
execute "dmd" with "subdir" as working directory.
-- 
Kind Regards
Benjamin Thaut
Oct 23 2013
parent reply "simendsjo" <simendsjo gmail.com> writes:
On Thursday, 24 October 2013 at 06:25:40 UTC, Benjamin Thaut 
wrote:
 As far as I can tell std.process can only run commands in the 
 working directory of the currently executing function. I want 
 to execute a certain program inside a subdirectory on windows 
 and can't get it to work:

 myproject
  |- subdir

 So my executable has "myproject" as working directory. And I 
 want to execute "dmd" with "subdir" as working directory.
Isn't it possible to execute a command like "cd subdir && dmd"?
Oct 24 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 24.10.2013 09:06, schrieb simendsjo:
 On Thursday, 24 October 2013 at 06:25:40 UTC, Benjamin Thaut wrote:
 As far as I can tell std.process can only run commands in the working
 directory of the currently executing function. I want to execute a
 certain program inside a subdirectory on windows and can't get it to
 work:

 myproject
  |- subdir

 So my executable has "myproject" as working directory. And I want to
 execute "dmd" with "subdir" as working directory.
Isn't it possible to execute a command like "cd subdir && dmd"?
Thanks. That works. It still would be nice if std.process had this in its API. -- Kind Regards Benjamin Thaut
Oct 24 2013
parent reply Timothee Cour <thelastmammoth gmail.com> writes:
+1
this is a command use case. Further,relying on shell such as  cd subdir &&
foo is fragile: if it fails, we're not sure whether it's because it
couldn't cd to subdir or because of foo.

Woudl the following be as efficient?
system_in_dir(string dir, string action){
auto path=getcwd
scope(exit)
  chdir(path)
chdir(dir)
system(action)
}


On Thu, Oct 24, 2013 at 12:43 AM, Benjamin Thaut <code benjamin-thaut.de>wrote:

 Am 24.10.2013 09:06, schrieb simendsjo:

  On Thursday, 24 October 2013 at 06:25:40 UTC, Benjamin Thaut wrote:
 As far as I can tell std.process can only run commands in the working
 directory of the currently executing function. I want to execute a
 certain program inside a subdirectory on windows and can't get it to
 work:

 myproject
  |- subdir

 So my executable has "myproject" as working directory. And I want to
 execute "dmd" with "subdir" as working directory.
Isn't it possible to execute a command like "cd subdir && dmd"?
Thanks. That works. It still would be nice if std.process had this in its API. -- Kind Regards Benjamin Thaut
Oct 24 2013
parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 24.10.2013 19:03, schrieb Timothee Cour:
 +1
 this is a command use case. Further,relying on shell such as  cd subdir
 && foo is fragile: if it fails, we're not sure whether it's because it
 couldn't cd to subdir or because of foo.

 Woudl the following be as efficient?
 system_in_dir(string dir, string action){
 auto path=getcwd
 scope(exit)
    chdir(path)
 chdir(dir)
 system(action)
 }
Well at least the windows API call CreateProcess already has a parameter which you can use to specify the working directory. Its just not exposed in the implementation of std.process. I don't know how things are on linux though. Kind Regards Benjamin Thaut
Oct 24 2013