- NSIS Discussion
- Search for text in file?
Archive: Search for text in file?
v47
29th July 2012 11:23 UTC
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
29th July 2012 12:47 UTC
You can probably use LineFind from filefunc.nsh. http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.2
v47
29th July 2012 19:44 UTC
any chance of an example?
MSG
29th July 2012 19:58 UTC
There are many usage examples in the documentation I linked.
T.Slappy
30th July 2012 06:24 UTC
Is your config file INI based?
ReadINIStr - http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2.10
v47
31st July 2012 10:00 UTC
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
31st July 2012 12:57 UTC
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
31st July 2012 14:01 UTC
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
31st July 2012 14:10 UTC
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
31st July 2012 14:27 UTC
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
31st July 2012 14:29 UTC
You should be able to use ConfigRead for this (it's in the manual).
Stu
v47
31st July 2012 14:37 UTC
I'll try, thanks.
Afrow UK
31st July 2012 14:52 UTC
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
31st July 2012 15:46 UTC
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
31st July 2012 16:00 UTC
$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
31st July 2012 17:00 UTC
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.
v47
1st August 2012 12:09 UTC
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
28th October 2012 06:13 UTC
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
|
MSG
28th October 2012 07:14 UTC
Simply add a counter to the loop. Start at zero before ${Do}, then +1 just before you ${Loop}