Skip to content
⌘ NSIS Forum Archive

Search for text in file?

19 posts

v47#

Search for text in file?

hello.

just started to use NSIS, and I would like to do following:

open "settings.cfg" in the $INSTDIR
look for "german", if found, copy german.cfg to $INSTDIR
look for "french", if found, copy french.cfg to $INSTDIR

tried to play with this http://nsis.sourceforge.net/Search_for_text_in_file for a bit, but I'm totally lost (on the look for/if found part). help?

thank you.
MSG#
You can probably use LineFind from filefunc.nsh. http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.2
T.Slappy#
Is your config file INI based?
ReadINIStr - http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2.10
v47#
no, it's a cfg, but it's just a renamed txt file. tried to look at those examples, but it's way too complicated for me - until now, all I had to do was check for files and move them accordingly, and this is the first time I have to do something more complicated.. I was hoping for something I could just copy & paste, no luck I guess.
Afrow UK#
T.Slappy wanted to know the format of the file. 'cfg' is just a generic extension for a configuration file, whereas an INI file has a very specific format. What does the actual format/contents look like?

Stu
v47#
no, it's just a text file with variables. no specific format.

the plan I had was doing a text/string lookup in the file, and then using some if text/string exists function, just like I have been using IfFileExists for all the previous tasks, but I see no such thing here (with my very unskilled eye, anyway).

maybe I should just make this user selectable and forget about it..
Afrow UK#
no, it's just a text file with variables
What do you mean exactly?

If there is no specific format, how can you look up a string? We cannot help you if we do not know what the format is.

Stu
v47#
inst.cfg, contents:
install_path .\
language english
the problem is, the user can have either english, german or french in the language line, and some files that have to be copied over are language specific, and the installer needs to know when german or french is used as some extra files need to be copied over (english is not a problem, it's the default language so no extra files are needed).
Afrow UK#
Or you can use this code:
FileOpen $R0 path\to\inst.cfg r
${Do}

FileRead $R0 $R1
${If} ${Errors}
${Break}
${EndIf}

StrCpy $R2 $R1 13
${If} $R2 == `install_path `
StrCpy $CfgInstallPath $R1 `` 13
${TrimNewLines} $CfgInstallPath $CfgInstallPath
${Continue}
${EndIf}

StrCpy $R2 $R1 9
${If} $R2 == `language `
StrCpy $CfgLanguage $R1 `` 9
${TrimNewLines} $CfgLanguage $CfgLanguage
${Continue}
${EndIf}

${Loop}
FileClose $R0
Stu
v47#
ugh, I'm totally lost on this - can't figure your out your piece of code or what is the $var in ConfigRead supposed to do.

guess I bit off more than I can chew.
Afrow UK#
$var is what it says - a variable which receives the output/value. It can be one of NSIS's built in ones ($0-$9, $R0-$R9) or one you define yourself using the Var instruction. My code uses two variables $CfgInstallPath and $CfgLanguage which would need to be defined with the Var instruction.

Stu
MSG#
Originally Posted by Afrow UK View Post
two variables $CfgInstallPath and $CfgLanguage which would need to be defined with the Var instruction.
Correction to avoid confusion: "Need to be declared with the Var instruction." Defining is done with !define, which is completely different.
v47#
received help from a fellow forum member. adding the function he made, should anyone need something similar in the future:

Function CheckLanguage
FileOpen $R0 "$INSTDIR\install.cfg" r
Var /GLOBAL CfgLanguage
${DO}
FileRead $R0 $R1
${If} ${Errors}
${Break}
${EndIf}

StrCpy $R2 $R1 9
${If} $R2 == `language `
StrCpy $CfgLanguage $R1 `` 9
${Break}
${EndIf}
${Loop}

${Switch} $CfgLanguage
${Case} 'english'
File /oname=install.cfg files\English_install.cfg
${Break}

${Case} 'german'
File /oname=install.cfg files\German_install.cfg
${Break}

${Default}
File /oname=install.cfg files\English_install.cfg
${Break}
${EndSwitch}
FunctionEnd

thanks to everybody who helped. 🙂
PoRtAbLe_StEaLtH#
Thank You

Originally Posted by Afrow UK View Post
Or you can use this code:
FileOpen $R0 path\to\inst.cfg r
${Do}

FileRead $R0 $R1
${If} ${Errors}
${Break}
${EndIf}

StrCpy $R2 $R1 13
${If} $R2 == `install_path `
StrCpy $CfgInstallPath $R1 `` 13
${TrimNewLines} $CfgInstallPath $CfgInstallPath
${Continue}
${EndIf}

StrCpy $R2 $R1 9
${If} $R2 == `language `
StrCpy $CfgLanguage $R1 `` 9
${TrimNewLines} $CfgLanguage $CfgLanguage
${Continue}
${EndIf}

${Loop}
FileClose $R0
Stu

How hard would it be to also get line number of found string?