0

I'm working through how to change the path environment variable using the NSIS plug in.

https://nsis.sourceforge.io/EnVar_plug-in

I'm able to install the plug in but I'm not seeing in the example provided how to edit the path variable. How do we

  • Delete an existing string in the path
  • Append a string to the path
  • Prepend a string to the path
  • Check for the existence of a string in the path
  • Delete that string from the path if it exists
John
  • 3,458
  • 4
  • 33
  • 54

2 Answers2

1

The functions with Value in their name (and ::Check) have automatic semicolon (separator) handling.

!include LogicLib.nsh
Section
EnVar::SetHKCU
EnVar::Check "Path" "$InstDir"
Pop $0
${If} $0 = 0
  DetailPrint "Already there"
${Else}
  EnVar::AddValue "Path" "$InstDir"
  Pop $0 ; 0 on success
${EndIf}

EnVar::DeleteValue "Path" "$InstDir"
Pop $0
SectionEnd

The %path% variable is a shared resource, you don't get to decide the order.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Unfortunate that we can't decide the order. I'm adding a path to a Java version that is required for the install and would really like to pre-pend the "correct" (for the application being installed - which is the only application that should be running on the machine) version of the jre to the path. – John Sep 30 '22 at 17:33
  • 1
    If it is the only application on the machine, why would the order matter? The only things present by default is basically just `c:\windows;c:\windows\system32`. – Anders Sep 30 '22 at 18:25
  • It's supposed to be the only thing. Developers have all kinds of junk on their machines including multiple Java versions. When they get transitioned to this project, part of the cleanup is to get them on the version of Java used by this application. One of the goals of this build was to automate this process. How would I go about automating making Java 11.0.11 the current version of Java (including making sure it is the first JVM in the class path? – John Sep 30 '22 at 19:27
0

I was able to figure out everything but how to prepend a value to the path.
Helpful information is here:

https://nsis.sourceforge.io/EnVar_plug-in

API documentation for Env:: is available here:

https://github.com/GsNSIS/EnVar

The code below creates a current user environment variable, deletes a value from it, and then adds a new value to it. I'm assuming I can edit the "path" environment variable by replacing "DELETE_ME" with "path". Full example is here: https://github.com/NACHC-CAD/nsis-examples

# ---
#
# This script shows some basic functionality of the EnVar plug in.  
# Based on https://nsis.sourceforge.io/Setting_Environment_Variables_Examples
#
# WriteEnvStr.nsh was downloaded from:
# https://github.com/rasa/nsislib/blob/master/WriteEnvStr.nsh
# 
# ---

# definitions
Outfile "005-envar-examples-installer.exe"
InstallDir "C:\temp\nsis-examples\005-envar-examples-installer"

Page Directory
Page InstFiles

# section to copy files and manipulate env variable
Section

    # copy files
    DetailPrint ""
    DetailPrint "Copying files to $InstDir..."
    DetailPrint ""
    SetOutPath "$InstDir\resources"
    File /a /r "resources\"

    #
    # create an environment variable
    #

    # Create the variable.  
    # If you run this script twice, you well get repetes of the existing_path values, i.e. the behavior seems to be append if exists.  
    DetailPrint ""
    DetailPrint "Creating DELETE_ME environment variable..."
    EnVar::SetHKCU
    EnVar::AddValue "DELETE_ME" "C:\existing_path_1;C:\existing_path_2;C:\existing_path_3;"

    # check to see if the variable was created
    EnVar::Check "DELETE_ME" "NULL"
    Pop $0
    DetailPrint "EnVar::Check returned=|$0| (should be 0)"  

    # add a new string to the existing variable
    DetailPrint ""
    DetailPrint "Appending new path to DELETE_ME..."
    EnVar::AddValue "DELETE_ME" "D:\NEW_PATH;"
    Pop $0
    DetailPrint "EnVar::Check returned=|$0| (should be 0)"  

    # remove a string from the existing variable
    DetailPrint ""
    DetailPrint "Deleting C:\existing_path_3"
    EnVar::DeleteValue "DELETE_ME" "C:\existing_path_3;"
    Pop $0
    DetailPrint "EnVar::Check returned=|$0| (should be 0)"  

    #
    # done
    #
    
    DetailPrint ""
    DetailPrint "Done."


SectionEnd
John
  • 3,458
  • 4
  • 33
  • 54