Francesco Esposto
Francesco Esposto

Reputation: 71

Pass a variable into File function in for loop cycle in NSIS

my problem is this: I cannot pass variable inside a for loop, it seems that it does not recognize the variable. It takes the string with the variable, but not the variable value. This is my code

!define FORMAT "XY 2018|XY 2019"

!include "LogicLib.nsh"
!include "Explode.nsh"

# set the name of the installer
Outfile "example.exe"

# create a default section.
Section

${Explode}  $0  "|" "${FORMAT}"
${For} $1 1 $0
    Pop $2
    !define PATH_OK "Z:\XY\Doc\List\*$2*"
    File "${PATH_OK}"
${Next}

SectionEnd

When I'm going to compile the nsis file, it returns this error:

!define: "PATH_OK"="Z:\XY\Doc\List\*$2*"
File: "Z:\XY\Doc\List\*$2*" -> no files found.
Usage: File [/nonfatal] [/a] ([/r] [/x filespec [...]] filespec [...] |
/oname=outfile one_file_only)
Error in script "C:\Users\Francesco\Desktop\example.nsi" on line 19 aborting creation process

I can't understand why... if I try to change the code File "${PATH_OK}" with a messagebox the path result ok. Where am I doing wrong? Many thanks to all!

Upvotes: 1

Views: 874

Answers (3)

Anders
Anders

Reputation: 101736

You cannot mix and match compile-time and run-time instructions! You should also learn the difference between a ${define} and a $variable.

All instructions starting with ! are processed by the pre-processor. On the other hand, ${For} runs on the end-users machine and that is too late for File to add files to your installer.

File only supports basic DOS wildcards (* and ?):

Section
SetOutPath $InstDir
File "c:\mysourcefiles\XY 201?.txt"
SectionEnd

If you need something more advanced then you must use a macro or a external helper program executed with !system.

!define FORMAT "XY 2018.txt|XY 2019.txt"

!macro ExplodeAndIncludeFile string sep
!searchparse /noerrors "${string}" "" ExplodeAndIncludeFile_Temp "${sep}"
!ifdef ExplodeAndIncludeFile_Temp
    File "${ExplodeAndIncludeFile_Temp}"
    !undef ExplodeAndIncludeFile_Temp
    !searchparse /noerrors "${string}" "${sep}" ExplodeAndIncludeFile_Temp
    !ifdef ExplodeAndIncludeFile_Temp
        !insertmacro ${__MACRO__} "${ExplodeAndIncludeFile_Temp}" "${sep}" ; Macro recursion only supported in NSIS 3+
    !endif
!endif
!macroend

Section
SetOutPath $InstDir
!insertmacro ExplodeAndIncludeFile "${FORMAT}" "|"
SectionEnd

Upvotes: 1

idleberg
idleberg

Reputation: 12882

!define is a compile time command, while variables are defined and evaluated at runtime.

From the documentation:

Compiler Utility Commands

These commands are similar to the C preprocessor in terms of purpose and functionality. They allow file inclusion, conditional compilation, executable header packing and process execution during the build process. Note: None of these commands allow the use of variables.

Also, unlike defines, variables don't work with the File command.

Upvotes: 0

KennZAney1
KennZAney1

Reputation: 91

You are using an illegal character (*) in the file name.

Reference: Naming Files, Paths, and Namespaces

Upvotes: 0

Related Questions