Andrei N
Andrei N

Reputation: 21

StrContains function arguments shows error "Invalid command" for variable

I am trying to use NSIS StrContains command.

According to the wiki (https://nsis.sourceforge.io/StrContains) its arguments are following: ${StrContains} "$result_var" "Needle" "Haystack"

and example is following:

${StrContains} $0 "Dirt" "Dirty deeds done dirt cheap"

But when I try to use this function the compiler shows me an error:

Invalid command: $ContainsText

I tried to change variable to stack $0, but result is the same. Seem that the problem is in arguments, but arguments reordering does not helps. Including variable to quotes also does not have any effect.

I used NSIS 3.01, then updated to the latest version 3.04 - result is the same.

Here is my test *.nsi file:

;Include Modern UI
  !include "MUI2.nsh"
; Include for String commands
  !include "LogicLib.nsh"
;--------------------------------
;General

  ;Name and file
  Name "Basic Test"
  OutFile "Basic.exe"

  ;Default installation folder
  InstallDir "C:\temp"

  ;Request application privileges
  RequestExecutionLevel user

  ; import functions
  !define StrContains

;--------------------------------
;Interface Settings
  !define MUI_ABORTWARNING
;--------------------------------
;Pages
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
;--------------------------------
;Languages
  !insertmacro MUI_LANGUAGE "English"
;--------------------------------
;Installer Sections

Var ContainsText

Section "Dummy Section" SecDummy

  SetOutPath "$INSTDIR"
  ;set 5 as initial value
  StrCpy $ContainsText "5"
  MessageBox MB_OK "Initial value: $ContainsText"

  ; check if inst dir contains 'temp'
  ; According to https://nsis.sourceforge.io/StrContains
  ; ${StrContains} "$result_var" "Needle" "Haystack"
  ${StrContains} $ContainsText $INSTDIR "temp"
  MessageBox MB_OK "Cmp result: $ContainsText"

  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"

SectionEnd

;--------------------------------
;Uninstaller Section
Section "Uninstall"
  Delete "$INSTDIR\Uninstall.exe"
  RMDir "$INSTDIR"
SectionEnd

I expect that first argument is a result variable, second - original string, third - searching substring. StrContains variable should be 'test' as a result, but this does not compile.

Please help me to understand why. Thanks in advance.

Upvotes: 2

Views: 1191

Answers (2)

Santanu Karar
Santanu Karar

Reputation: 1076

I had the same problem recently.

Initially, it was throwing Invalid command: "$0". Later I learned that an undefined ${StrContains} causing this trouble.

The documentations are very unclear, I was hoping to include/define a StrContains class-file in my script-file that should work. But none worked so far. I have seen very old discussions where !include StrContains.nsh been called - but that does not work anymore with the recent version of NSIS.

Finally what has worked this for me is to add the complete "Function Code" from the StrContains documentation page. I didn't expect this nor it was clear from the documentation. But this worked for me, and I able to call ${StrContains} way in my script file.

Upvotes: 0

KennZAney1
KennZAney1

Reputation: 91

Andrei.

It looks like you have the arguments in the wrong order. Also, you were correct in changing the variable to $0.

The Usage is:

${StrContains} "$result_var" "Needle" "Haystack"

where "$result_var" =$0, "Needle" = "temp" (the Needle you are looking for), and "Haystack" = $INSTDIR (the Haystack you are looking through).

So, instead of:

${StrContains} $ContainsText $INSTDIR "temp"

Try this:

${StrContains} $0 "temp" $INSTDIR

Upvotes: 0

Related Questions