John
John

Reputation: 3996

How to use NSIS EnVar plug in to edit path?

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

Upvotes: 0

Views: 677

Answers (2)

John
John

Reputation: 3996

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

Upvotes: 0

Anders
Anders

Reputation: 101764

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.

Upvotes: 1

Related Questions