Thread overview
Changing Environment Paths using D language's Phobos Library
Jun 29, 2019
BoQsc
Jun 29, 2019
BoQsc
Jun 29, 2019
BoQsc
Jun 29, 2019
Adam D. Ruppe
Jun 29, 2019
Paul Backus
June 29, 2019
I would like to remove/add/change Environment Paths using D language's Phobos.
https://en.wikipedia.org/wiki/PATH_(variable)

I saw in the documentation that there is a class file "std.process : environment" that has the supposed ability to minipulate environment variables.
https://dlang.org/library/std/process/environment.html

I couldn't find a good documentation on how to search and replace strings using Phobos Library. However it seems that it might be possbile by importing "std.array : replace" function.
https://forum.dlang.org/post/jnua50$1e4a$1@digitalmars.com
https://dlang.org/library/std/array/replace.html

So, here we are, testing if it is possible to retrieve and set the Environmental Variables.

> import std.stdio   : writeln;
> import std.array   : replace;
> import std.process : environment;
>
>void main(){
>
>
>	// Retrieve environment variable "path" for testing if it is possible
>	// to retrieve it by using D language Phobos Library
>	auto environmentPaths = environment.get("path");
>	writeln(environmentPaths); // Works as expected: we can see the output of Paths.
>	
>	// Create a new Environement variable for testing if it is possible
>	// to Create it with D language Phobos Library
>	environment.opIndexAssign("variableName", "Some Random Value Here");
>	auto environmentalVariable = environment.get("variableName");
>	writeln(environmentalVariable); //Unexpected: absolutely no output.
	
	
	

Alright, now that we see it is possible to retrieve environmental variable, that's great.

However, setting the variable via "environment.opIndexAssign" seems to not work.
What should we do?
I don't know.

June 29, 2019
On Saturday, 29 June 2019 at 09:28:03 UTC, BoQsc wrote:
>
> However, setting the variable via "environment.opIndexAssign" seems to not work.
> What should we do?
> I don't know.

I'm glad I'm on this forum, I know exactly what you did wrong mr. BoQsc

I checked the documentation and it seems that, you assumed the wrong syntax for "environment.opIndexAssign()"

The correct syntax is:
>environment.opIndexAssign("Some Random Value Here", "variableName");

And not this one:
>environment.opIndexAssign("variableName", "Some Random Value Here");


Take a look at this documentation once more:
https://dlang.org/library/std/process/environment.op_index_assign.html

>import std.stdio   : writeln;
> import std.array   : replace;
> import std.process : environment;
>
>void main(){
>	// Retrieve environment variable "path" for testing if it is possible
>	// to retrieve it by using D language Phobos Library
>	auto environmentPaths = environment.get("path");
>	writeln(environmentPaths); // Works as expected: we can see the output of Paths.
>	
>	// Create a new Environement variable for testing if it is possible
>	// to Create it with D language Phobos Library
>	environment.opIndexAssign("Some Random Value Here", "variableName");
>	auto environmentalVariable = environment.get("variableName");
>	writeln(environmentalVariable); //Unexpected: absolutely no output.
>}
June 29, 2019
On Saturday, 29 June 2019 at 09:50:12 UTC, BoQsc wrote:
> On Saturday, 29 June 2019 at 09:28:03 UTC, BoQsc wrote:
> I checked the documentation and it seems that, you assumed the wrong syntax for "environment.opIndexAssign()"
>
> The correct syntax is:
>>environment.opIndexAssign("Some Random Value Here", "variableName");
>
> And not this one:
>>environment.opIndexAssign("variableName", "Some Random Value Here");
>
>
> Take a look at this documentation once more:
> https://dlang.org/library/std/process/environment.op_index_assign.html

You are absolutely correct, however, it seems that these new variables that we create by using
>environment.opIndexAssign("Some Random Value Here", "variableName");


Are not accessible in Windows 10 settings, at all, as far as I see now.
Is that suppose to be that way?

I tried to open command prompt and the environment variable that we just created using D lang Phobos "opIndexAssign()" function seems to be not accessible nor created.

What is happening, does anyone know?
June 29, 2019
On Saturday, 29 June 2019 at 10:01:35 UTC, BoQsc wrote:
> Are not accessible in Windows 10 settings, at all, as far as I see now.
> Is that suppose to be that way?

A program can only modify its own environment variables. These modifications are passed to its children, but not back up to its parents.

Any changes you make will not be saved by the operating system since that is a parent of your program instead of its children.


if you do wanna make permanent changes i believe that means poking the registry and requires associated permissions.
June 29, 2019
On Saturday, 29 June 2019 at 09:50:12 UTC, BoQsc wrote:
> The correct syntax is:
>>environment.opIndexAssign("Some Random Value Here", "variableName");
>
> And not this one:
>>environment.opIndexAssign("variableName", "Some Random Value Here");

Note that opIndexAssign is an operator overload [1], so you can also write it like this:

environment["variableName"] = "Some Random Value Here";

[1] https://dlang.org/spec/operatoroverloading.html#index_assignment_operator