Jump to content

Using SETX


Jango
 Share

Recommended Posts

Hi,

I'm using this code in a script:

$Result = ""
For $i = 1 To $Arr[0][0]
    RunWait($ExePath & "\SETX.EXE " & $Arr[$i][0] & " " & StringReplace($Arr[$i][1], "%CFGPATH%", $CfgPath) & " -m", $ExePath, @SW_HIDE)
    EnvUpdate()
    Sleep(2000)
    $Result = $Result & $Arr[$i][0] & " = " & EnvGet($Arr[$i][0]) & @CRLF
Next

MsgBox(0, $Title, "Variables d'environnement" & @CRLF & $Result)

The SETX doc is here SETX

After the SETX, i read the variable with EnvGet but it return "" for all variables. But the Environment Variable are set well (i can see it if i right click on the computer then properties then advanced then Environment Variable)

If i restart my script EnvGet read the env var correctly ... any idea ?

Link to comment
Share on other sites

Hi,

I'm using this code in a script:

$Result = ""
For $i = 1 To $Arr[0][0]
    RunWait($ExePath & "\SETX.EXE " & $Arr[$i][0] & " " & StringReplace($Arr[$i][1], "%CFGPATH%", $CfgPath) & " -m", $ExePath, @SW_HIDE)
    EnvUpdate()
    Sleep(2000)
    $Result = $Result & $Arr[$i][0] & " = " & EnvGet($Arr[$i][0]) & @CRLF
Next

MsgBox(0, $Title, "Variables d'environnement" & @CRLF & $Result)

The SETX doc is here SETX

After the SETX, i read the variable with EnvGet but it return "" for all variables. But the Environment Variable are set well (i can see it if i right click on the computer then properties then advanced then Environment Variable)

If i restart my script EnvGet read the env var correctly ... any idea ?

You don't need SETX.exe because you can just do this:

Global $Arr[3][2] = [[2, ""], ["TestVar1", "TestData1"  ], ["TestVar2", "TestData2"  ]]
$Result = ""
For $i = 1 To $Arr[0][0]
    RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment",$Arr[$i][0], "REG_SZ", $Arr[$i][1])
    EnvUpdate()
    Sleep(2000)
    $Result &= $Arr[$i][0] & " = " & EnvGet($Arr[$i][0]) & @CRLF
Next

MsgBox(0, "Env Test", "Variables d'environnement" & @CRLF & $Result)

As for EnvUpdate()... well, it doesn't. The environment your script is running in remains unchanged as far as I can tell. But the values are there for any new apps that get run, or if you close and reopen your script, they will be there.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

As for EnvUpdate()... well, it doesn't. The environment your script is running in remains unchanged as far as I can tell. But the values are there for any new apps that get run, or if you close and reopen your script, they will be there.

Ok thank you, so there is no way to read the env var i just created in the same script ?

Link to comment
Share on other sites

Ok thank you, so there is no way to read the env var i just created in the same script ?

RegRead() at the same keys shown above.

Remember this is only in the current shell where you created/changed them. As soon as a new environment is started, like exiting and restarting your script, they will be there for EnvGet().

I had a terrible time with this in one of my scripts. I have to make changes to the environment %PATH% that are required for this particular app. Then I run the app and make a bunch of config changes by scripting its GUI. But the app won't start because the change to %PATH% is not reflected in the current shell, or any shell spawned by the current shell. VERY annoying, and EnvUpdate() doesn't help.

Things were moving too fast at the time to carefully work out an elegant solution. I just prompted the user to launch the app. Since it wasn't spawned by the current script's process, it picks up the new environment and works fine.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I had a terrible time with this in one of my scripts. I have to make changes to the environment %PATH% that are required for this particular app. Then I run the app and make a bunch of config changes by scripting its GUI. But the app won't start because the change to %PATH% is not reflected in the current shell, or any shell spawned by the current shell. VERY annoying, and EnvUpdate() doesn't help.

Use EnvSet before running the app within the same script and it should inherit the new setting for %PATH%.

:)

Link to comment
Share on other sites

Use EnvSet before running the app within the same script and it should inherit the new setting for %PATH%.

:)

Thank you everybody i found a solution, when my script start i check if my env var exist, if not i create it and exit, then ask the user to restart the script.

When my script restart it detect env var already exists then continue the rest of the script.

Link to comment
Share on other sites

Thank you everybody i found a solution, when my script start i check if my env var exist, if not i create it and exit, then ask the user to restart the script.

When my script restart it detect env var already exists then continue the rest of the script.

Check out the reply from MHz. I think what he is saying is to do BOTH -- set the System Environment with the reg hack I showed you, and then also use EnvSet() to include them in the currently running environment. New shells will get it from the System Environment, spawned shells from the current one will get it from EnvSet().

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...