Sign in to follow this  
Followers 0
VeeDub

Problem setting the PATH

8 posts in this topic

Hi,

I am using v3. I developed the script on XP where it seemed to work fine. I'm now trying to run it on a W2K box where it seems to be broken B)

I'm having trouble updating the PATH environment, I've searched through the forum and haven't found any similar symptoms so I am probably doing something dumb.

The code I am using is:

$Existing_Path = EnvGet("PATH")

$New_Path = $Existing_Path & ";" & """" & $App_Path1 & """" & ";" & """" & $App_Path2 & """" & ";"

EnvSet("PATH",$New_Path)

If I do an ENVGET on PATH after the above, the 'New_Path' is returned, all well and good ... unfortunately not

Later on when I try to call my app

$Command = @ComSpec & " /k " & "app.exe"

Run($Command, "",)

It can't find the app. When you check the PATH, it's the original path.

Would appreciate any ideas on why this might be.

Thanks,

VeeDub

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

StringSplit the path and loop it to see if the file exists. Run it if it does.

Global $syspath 
$path = StringSplit(EnvGet('path'), ';')
If Not @error Then
    For $i = 1 To $path[0]
        If FileExists($path[$i] & '\app.exe') Then
            $syspath = $path[$i]
            If StringRight($syspath, 1) = '\' Then
                $syspath = StringTrimRight($syspath, 1)
            EndIf
            ExitLoop
        EndIf
    Next
EndIf
If $syspath Then
    $Command = @ComSpec & " /k " & $syspath & "\app.exe" 
    Run($Command)
EndIf

Edit: This is not a EnvSet() solution. I misjudged the inital post.

Edited by MHz

Share this post


Link to post
Share on other sites

EnvSet() is a temporary path set by the script. It is not a permanet path as you are trying to use.

EnvSet("MYENV", "this is a test")
MsgBox(0, '', EnvGet('MYENV'))
Sleep(1000)

From Helpfile:

Remarks

A environment variable set in this way will only be accessible to programs that AutoIt spawns (Run, RunWait). Once AutoIt closes, the variables will cease to exist.

Share this post


Link to post
Share on other sites

Hi MHz,

Thanks for having a look at this ...

From Helpfile:

Remarks

A environment variable set in this way will only be accessible to programs that AutoIt spawns (Run, RunWait). Once AutoIt closes, the variables will cease to exist.

I'm not sure that I follow you, I am trying to set the path and then use AutoIt to spawn a program, so therefore the update to the PATH ought to be available to any program that I call via my AutoIt script?

What puzzles me is that as the code works fine on the XP box, so I would expect it to work on the W2K box as well, but it doesn't.

VeeDub

Share this post


Link to post
Share on other sites

To permanently set environment variables, you can write directly to the registry:

HKEY_CURRENT_USER\Environment

for current user, or

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment

for machine environment variables.

They will NEVER be available to the program that set it, and won't be available until the registry "refreshes" (does anyone know how to do that?).

With this method, you can also set an environment variable to the value of another environment variable such as

RegWrite ( 'HKEY_CURRENT_USER\Environment', 'MyVar', 'REG_EXPAND_SZ', '%userprofile%' )

just make sure you use REG_EXPAND_SZ instead of REG_SZ.

There's a Microsoft utility - SETX - which can also do this, and refreshes the registry. Unfortunately, the variable will NOT be available to the program that sets it. You'd have to use both EnvSet and either SETX or writing to the registry to use it in a program and make it permanently available. Get the utility from:

http://www.microsoft.com/windows2000/techi...ting/setx-o.asp

Share this post


Link to post
Share on other sites

Hi JerryD,

A comprehensive reply ... thank you.

I may yet adopt the approach you outline, before I do though, I am going to experiment with FileChangeDir to set the working directory before I call my app.

The reason that I have to muck around with all this path management, rather than just calling the app with the full path, is that the app's are cygwin ports. I need to call them via comspec so that I can redirect the output to log files ... and for some reason when I call via comspec with the full path - no matter what I try with quotes I can't get "Program Files" to be parsed correctly.

The work-around in XP was to set the path before-hand, and then just call the app. Maybe I can use FileChangeDir in both environments. It's late now, so I'll test this idea tomorrow ... but if that doesn't work I might have to use SETX.

Have to say though that I have only been using AutoIt for a very short time, and am very impressed with the development environment and the editor. Really cool.

cheers,

VeeDub

Share this post


Link to post
Share on other sites

This is what I currently use to add to a permanent path

Example

_Path('System', '%SystemRoot%\SystemFiles')

The UDF

Func _Path( $type, $value)
; adds a folder to permanently become part of the systems path.
    If $type = 'System' Then
        Local $environment = 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
    ElseIf $type = 'User' Then
        Local $environment = 'HKCU\Environment'
    Else
        Return
    EndIf
    Local $old = RegRead( $environment, 'Path'), $semicolon = ''
    If $old <> '' Then $semicolon = ';'
    If Not StringInStr( $old, $value) Then
        Local $new = $old & $semicolon & $value
        If RegWrite( $environment, 'Path', 'Reg_Expand_sz', $new) Then EnvUpdate()
    EndIf
EndFunc

To use this immediately, just use EnvUpdate() to refresh the environment variables. This will set a permanent path for your program if you wish for that.

Share this post


Link to post
Share on other sites

MHz,

Your code works fine on the W2K box.

Thanks a bunch!

Cheers,

VW

Share this post


Link to post
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
Sign in to follow this  
Followers 0