Skip to main content

NSIS split string

; example2.nsi
;
; This script is based on example1.nsi, but it remember the directory,
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install example2.nsi into a directory that the user selects.
;
; See install-shared.nsi for a more robust way of checking for administrator rights.
; See install-per-user.nsi for a file association example.

;--------------------------------

; The name of the installer
Name "Example2"

; The file to write
OutFile "example2.exe"

; Request application privileges for Windows Vista and higher
RequestExecutionLevel admin

; Build Unicode installer
Unicode True

; The default installation directory
InstallDir $PROGRAMFILES\Example2

; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\NSIS_Example2" "Install_Dir"

;--------------------------------

; Pages

Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;--------------------------------

; The stuff to install
Section "Example2 (required)"

SectionIn RO

; Set output path to the installation directory.
SetOutPath $INSTDIR

; Put file there
File "example2.nsi"

; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\NSIS_Example2 "Install_Dir" "$INSTDIR"

; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "DisplayName" "NSIS Example2"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "NoRepair" 1
WriteUninstaller "$INSTDIR\uninstall.exe"

SectionEnd

; Optional section (can be disabled by the user)

!include 'LogicLib.nsh'
!include strExplode.nsh
Section "Splite String"
!insertmacro "strExplode" $0 '.' '14.5.61'
${do}
Pop $1
${If} ${Errors}
;All Parts have been popped
ClearErrors
${ExitDo}
${Else}
DetailPrint "Part Value: $1"
${EndIf}
${loop}
SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"

; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2"
DeleteRegKey HKLM SOFTWARE\NSIS_Example2

; Remove files and uninstaller
Delete $INSTDIR\example2.nsi
Delete $INSTDIR\uninstall.exe

; Remove shortcuts, if any
Delete "$SMPROGRAMS\Example2\*.lnk"

; Remove directories
RMDir "$SMPROGRAMS\Example2"
RMDir "$INSTDIR"

SectionEnd

  • strExplode.nsh
;=====================================================================================
;=
;= Project Name: NSIS_DotNetVersion
;=
;= File Name: strExplode.nsh
;= File Version: 1.0 Beta
;=
;= Descritoin: NSIS Library used to split a string into an array based
;= on a separator character
;=
;=====================================================================================
;= Copyright (C) 2018 Peter Varney - All Rights Reserved
;= You may use, distribute and modify this code under the
;= terms of the MIT license,
;=
;= You should have received a copy of the MIT license with
;= this file. If not, visit : https://github.com/fatalwall/NSIS_strExplode
;=====================================================================================
;=
;= Usage:
;= strExplode Length Separator String
;=
;= Length Variable such as $0 that the resutl is returned in
;= Separator A single character which will be used to split the passed string
;= String Original String which is passed in
;=
;= Example 1 - Known count of elements returned:
;= !include strExplode.nsh
;= strExplode $0 '.' '4.7.1'
;= ${If} $0 == 3
;= Pop $1
;= Pop $2
;= Pop $3
;= DetailPrint "First Part: $1"
;= DetailPrint "Sectond Part: $2"
;= DetailPrint "Third Part: $3"
;= ${EndIf}
;=
;= Example 2 - Unknown count of elements returned:
;= !include strExplode.nsh
;= strExplode $0 '.' '4.7.1'
;= ${do}
;= Pop $1
;= ${If} ${Errors}
;= ;All Parts have been popped
;= ClearErrors
;= ${ExitDo}
;= ${Else}
;= DetailPrint "Part Value: $1"
;= ${EndIf}
;= ${loop}
;=
;=====================================================================================

!ifndef strExplode
!define strExplode "!insertmacro strExplode"


VAR EVAL_PART
VAR PARAM_SEPARATOR
VAR PARAM_STRING
VAR EVAL_CHAR

VAR INT_RETURN

!macro strExplode Length Separator String
Push `${Separator}`
Push `${String}`
!ifdef __UNINSTALL__
Call un.strExplode
!else
Call strExplode
!endif
Pop `${Length}`
!macroend

!macro Func_strExplode un
Function ${un}strExplode
ClearErrors

Pop $PARAM_STRING
Pop $PARAM_SEPARATOR
StrCpy $EVAL_PART '' ;ensur variable is blank
StrCpy $INT_RETURN 0 ; Initialize Counter
${Do}
StrCpy $EVAL_CHAR $PARAM_STRING "" -1 ;Get Last Character
${If} $EVAL_CHAR == ''
;End of string
IntOp $INT_RETURN $INT_RETURN + 1 ; Increment Counter
push $EVAL_PART
${ExitDo}
${ElseIf} $EVAL_CHAR == $PARAM_SEPARATOR
;Seperator found. Splitting
IntOp $INT_RETURN $INT_RETURN + 1 ; Increment Counter
push $EVAL_PART
StrCpy $EVAL_PART '' ; clear
${Else}
;add next character to item
StrCpy $EVAL_PART "$EVAL_CHAR$EVAL_PART"
${EndIf}
StrCpy $PARAM_STRING $PARAM_STRING -1 ;remove Last character
${Loop}

;Number of items string was split into
push $INT_RETURN
FunctionEnd
!macroend

!insertmacro Func_strExplode ""
!insertmacro Func_strExplode "un."

!endif