Skip to content
⌘ NSIS Forum Archive

Variable does not expand correctly in File

6 posts

tieum#

Variable does not expand correctly in File

Hello,

I try the following code

Var MyVar
StrCpy MyVar "C:\Path\myfile.txt"

SetOutPath "$INSTDIR"
File "$MyVar"

When I try to compile my installer I get the error message:

File "$MyVar" -> no files found.

Why is this?

If I try
File "C:\Path\myfile.txt"
it works.

Thanks,

Tieum
Comperio#
Variables are used at runtime. File is a compile-time command, so you can't use variables to define the file name.

You muse either use a 'real' name (File "C:\Path\myfile.txt") or you can use a symbol, like:
!define myFile "C:\path\mayfile.txt"
File ${myFile}

But, you can use variables to specify the output, such as:
File "/oname=$myFile" "C:\myfile.txt"
tieum#edited
The problem is !define is not "variable." I have documentation in several languages and need to be able to change the path where the documentation is taken from depending on the language on the fly. How can I achieve that?

Update just did it with !define and !undef. Thanks for the help.
Comperio#
I was just going to mention that if you wanted to control what gets installed, then here's a way that should work:
!include LogicLib.nsh
#...
Section
${Switch} $LANGUAGE
${Case} ${LANG_ENGLISH}
File "C:\English.txt"
${Break}
${Case} ${LANG_FRENCH}
File "C:\French.txt"
${Break}
${EndSwitch}
SectionEnd
But, if you instead wanted to build an separate installation for each language, then you could probably use !ifdef blocks for each language. Example:
!ifdef UseEnglish
; code for adding English files
!endif
edit You might also have a look at languages.nsi included in your nsis examples folder for more ideas.
umeca74#
it would be helpful if the FILE instruction error explained that variables cannot be used... I've been banging my head for a couple of hours on this weird fault 🙂
Anders#
Variables only work on the end-users machine, not your machine. "$Whatever" is a valid filename, there is no way for the compiler to guess if you wanted a variable or if that is just the name.