How to set environment variables for current account that can be used by other applications?
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Biraj Khanal
el 1 de Jul. de 2023
Comentada: Biraj Khanal
el 3 de Jul. de 2023
In a matlab script, I am trying to edit the environment variable , namely "Path" for the current user.
Now, getenv("Path") gives all the Path variable available including the system-wide path variables but I am only trying to access the current user's Path environment variable.
So I opted for:
current_path = winqueryreg('HKEY_CURRENT_USER','Environment','Path');
This returns the Path Variable just for current user as a character array which I can easily manipulate/edit, remove and add the desired path to it.
edited_path = edit_path(current_path);
My problem is : I want to set this character array as the Path variable for the current user. I tried doing it with the system command.
system('setx Path =edited_path')
However, it simply returns 0 and does not work . I even tried just a basic thing like :
system('setx Path ="test"');
This did not seem to work either.
I also tried with the set command instead of setx because it is just the current session that needs the environment variable but that did not work either.
Now, I could opt for setenv but I am not sure if this would affect the environment variables for other tools/applications.
The idea is : the script would run another application through a bat file that needs to use the changed environment variable.
Is there any way to go about this?
0 comentarios
Respuesta aceptada
Abhishek
el 3 de Jul. de 2023
Hi Biraj,
You can use the setx command along with the system function in MATLAB to set the environment variable "Path" for the current user. However, there are a few modifications you can try out to make it work.
Make sure to appropriately concatenate the edited_path variable with the system command string. Right now, rather than giving the variables value, you are passing the text edited_path.
In the system command, enclose the value of the edited_path variable in double quotations, this is necessary to handle paths with space and special characters.
% Get the current Path variable for the current user
current_path = winqueryreg('HKEY_CURRENT_USER', 'Environment', 'Path');
% Manipulate/edit the current_path variable as needed
edited_path = edit_path(current_path);
% Set the edited_path as the Path variable for the current user
system(['setx Path "', edited_path, '"']);
Further you can refer to the following documentation pages:
Más respuestas (0)
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!