Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Modify system PATH -- GUI


  • Please log in to reply
4 replies to this topic
infogulch
  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008
I wanted to be able to easily modify my systems' PATH entries (several computers). This is a simple gui script that makes it easy to view, edit, and add entries in your PATH.

Features:Validates that changes and new entries are a valid directories.
Expands environment variables in entries for validity checking. (Thanks, Lexikos)
Confirmation dialogs all along the way for potentially problematic changes.Note: in vista/7 this app needs to be run as Administrator. I'm too lazy to script it, so compile this script and check the "Run as Administrator" in the Compatibility tab in the compiled script's Properties.

#SingleInstance, Force
#NoEnv ; somewhat ironic...
	
	; this won't work in vista/7 if it's not run as an administrator.
	; i'm too lazy to request it manually so the easiest way is to 
	; compile this script, and then under Compatibility tab in the 
	; compiled exe's Properties select "Run as Administrator"
	
	RegRead, P, HKLM, SYSTEM\CurrentControlSet\Control\Session Manager\Environment, PATH
	
	Gui, +Delimiter`;
	
	width := 400
	Gui, Add, Text, w%width%, Double Click an entry to modify it. Click Add to make a new entry and Delete to remove the selected entry.
	Gui, Add, ListBox, vSysPath w%width% r8 +0x100 gEditEntry AltSubmit, %P%
	Gui, Font, s12
	Gui, Add, Button, gNew    w70 section  , New
	Gui, Add, Button, gDelete w70 ys xs+74 , Delete
	Gui, Add, Button, gExit   w70 ys xs+330, Cancel
	Gui, Add, Button, gSubmit w70 ys xp-74, Submit
	Gui, Show, , SysEnv -- Change System Environment Variable
return

EditEntry:
	if (a_guievent == "DoubleClick" && a_eventinfo) {
		Gui +OwnDialogs
		RegExMatch(P, "P)^(?:(?<E>[^;]*)(?:;|$)){" a_eventinfo "}", _)
		_En := IB( "Edit PATH entry", substr(P, _PosE, _LenE))
		if (ErrorLevel == 0)
		{
			if (InStr(FileExist(ExpEnv(_En)),"D"))
			{
				rP := substr(P, 1, _PosE-1) _En (_PosE+_LenE+1 < strlen(P) ? ";" substr(P, _PosE+_LenE+1) : "")
				P := rP
				GuiControl, , SysPath, `;%P%
			}
			else
				MsgBox, , SysEnv, Path is not a directory.
		}
	}
return

New:
	add := IB( "Add PATH entry" )
	if (instr(fileexist(ExpEnv(add)),"D")) {
		P .= (strlen(P) > 0 ? ";" : "") add
		GuiControl, , SysPath, `;%P%
	}
return

Delete:
	GuiControlGet, entry, , SysPath
	if (entry) {
		Gui, +OwnDialogs
		MsgBox, 4, SysEnv -- Confirm, Remove entry #%entry% from PATH?
		IfMsgBox Yes
		{
			RegExMatch(P, "P)^(?:(?<E>[^;]*)(?:;|$)){" entry "}", _)
			P := SubStr(P, 1, max(_PosE-2,0)) (_PosE+_LenE+1 < strlen(P) ? ";" substr(P, _PosE+_LenE+1) : "")
			GuiControl, , SysPath, `;%P%
		}
	}
return

Submit:
	Gui, +OwnDialogs
	MsgBox, 1, SysEnv -- Save Changes, Change system PATH to:`n`n%P%
	IfMsgBox, OK
	{
		RegWrite, REG_EXPAND_SZ, HKLM, SYSTEM\CurrentControlSet\Control\Session Manager\Environment, PATH, %P%
		If !ErrorLevel
			MsgBox, 0, SysEnv -- Success!, Modifying the PATH variable was successful!
		Else
			MsgBox,  , SysEnv, Error has occurred and new PATH variable was not saved.
	}
	Else
		MsgBox, , SysEnv -- Cancelled, Exiting now.

Exit:
GuiClose:
GuiEscape:
GuiEsc:
ExitApp

IB( prompt, default="" ) {
	InputBox, out, SysEnv -- %prompt%, %prompt%:, , , , , , , , %default%
	return out
}

max( a, b ) {
	return a > b ? a : b
}

ExpEnv(str) { 
	; by Lexikos: http://www.autohotkey.com/forum/viewtopic.php?p=327849#327849
	if sz:=DllCall("ExpandEnvironmentStrings", "uint", &str, "uint", 0, "uint", 0)
	{
		VarSetCapacity(dst, A_IsUnicode ? sz*2:sz)
		if DllCall("ExpandEnvironmentStrings", "uint", &str, "str", dst, "uint", sz)
			return dst
	}
	return ""
}
Yes I know it's possible to edit system variables but I'm also too lazy to drill down through a bunch of dialogs.

Posted Image

stevel
  • Members
  • 133 posts
  • Last active: Nov 15 2013 12:32 AM
  • Joined: 12 Oct 2010
Very useful. Thanks.

evgeni
  • Members
  • 1 posts
  • Last active: Aug 15 2013 10:38 AM
  • Joined: 15 Aug 2013

Thanks, this is very useful.

Except one limitation. After the script said that the variable got updated successfully (and I checked that it was, using Control Panel), opening Command Prompt still had the old PATH.

It seems that to complete the operation, you need to broadcast a WM_SETTINGCHANGE message as described here:

http://stackoverflow...rom-cmd-windows

 

I've replaced the MsgBox Success line by

                        {
                        ;WM_SETTINGCHANGE = 0x1A
                        ;See HWND_BROADCAST in
                        ;http://www.autohotkey.com/docs/commands/PostMessage.htm
                        SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF
                        MsgBox, 0, SysEnv -- Success!, Modifying the PATH variable was successful!
                        }

It works. Opening up Command Prompt just afterwards and "echo %PATH%" shows the updated PATH.

 

There is now a little delay after "Submit" is pressed and the messagebox shows up, clearly the message takes a while to get sent.

 

Evgeni Sergeev



gwarble
  • Members
  • 624 posts
  • Last active: Aug 12 2016 07:49 PM
  • Joined: 23 May 2009
You can probably PostMessage the WM_SettingChange instead of sendmessage

Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

Thanks, this is very useful.

Except one limitation. After the script said that the variable got updated successfully (and I checked that it was, using Control Panel), opening Command Prompt still had the old PATH.

It seems that to complete the operation, you need to broadcast a WM_SETTINGCHANGE message as described here:

http://stackoverflow...rom-cmd-windows

 

I've replaced the MsgBox Success line by

                        {
                        ;WM_SETTINGCHANGE = 0x1A
                        ;See HWND_BROADCAST in
                        ;http://www.autohotkey.com/docs/commands/PostMessage.htm
                        SendMessage, 0x1A,0,"Environment",, ahk_id 0xFFFF
                        MsgBox, 0, SysEnv -- Success!, Modifying the PATH variable was successful!
                        }

It works. Opening up Command Prompt just afterwards and "echo %PATH%" shows the updated PATH.

 

There is now a little delay after "Submit" is pressed and the messagebox shows up, clearly the message takes a while to get sent.

 

Evgeni Sergeev

+1


Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.