Archive: Search for text in file?


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.


You can probably use LineFind from filefunc.nsh. http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.2


any chance of an example?


There are many usage examples in the documentation I linked.


Is your config file INI based?
ReadINIStr - http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2.10


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.


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


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..


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

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).

You should be able to use ConfigRead for this (it's in the manual).

Stu


I'll try, thanks.


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

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.


$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


Originally posted by Afrow UK
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.

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. :)


Thank You
Quote:


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

Originally Posted by Afrow UK (Post 2874562) 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

Simply add a counter to the loop. Start at zero before ${Do}, then +1 just before you ${Loop}