Old 29th July 2012, 10:23   #1
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
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.
v47 is offline   Reply With Quote
Old 29th July 2012, 11:47   #2
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
You can probably use LineFind from filefunc.nsh. http://nsis.sourceforge.net/Docs/AppendixE.html#E.2.2
MSG is offline   Reply With Quote
Old 29th July 2012, 18:44   #3
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
any chance of an example?
v47 is offline   Reply With Quote
Old 29th July 2012, 18:58   #4
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
There are many usage examples in the documentation I linked.
MSG is offline   Reply With Quote
Old 30th July 2012, 05:24   #5
T.Slappy
Major Dude
 
T.Slappy's Avatar
 
Join Date: Jan 2006
Location: Slovakia
Posts: 530
Send a message via ICQ to T.Slappy
Is your config file INI based?
ReadINIStr - http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.2.10

Cool looking installer with custom design: www.graphical-installer.com
I offer NSIS scripting, C/C++/C#/Delphi programming: www.unsigned.sk
Develop NSIS projects in Visual Studio 2005-2019: www.visual-installer.com
or RAD Studio 2009, 2010, XE-10.3 Rio: www.rad-installer.com
T.Slappy is offline   Reply With Quote
Old 31st July 2012, 09:00   #6
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
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.
v47 is offline   Reply With Quote
Old 31st July 2012, 11:57   #7
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
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
Afrow UK is offline   Reply With Quote
Old 31st July 2012, 13:01   #8
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
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..
v47 is offline   Reply With Quote
Old 31st July 2012, 13:10   #9
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Quote:
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
Afrow UK is offline   Reply With Quote
Old 31st July 2012, 13:27   #10
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
inst.cfg, contents:
code:
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).
v47 is offline   Reply With Quote
Old 31st July 2012, 13:29   #11
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
You should be able to use ConfigRead for this (it's in the manual).

Stu
Afrow UK is offline   Reply With Quote
Old 31st July 2012, 13:37   #12
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
I'll try, thanks.
v47 is offline   Reply With Quote
Old 31st July 2012, 13:52   #13
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Or you can use this code:
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
Afrow UK is offline   Reply With Quote
Old 31st July 2012, 14:46   #14
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
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.
v47 is offline   Reply With Quote
Old 31st July 2012, 15:00   #15
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
$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
Afrow UK is offline   Reply With Quote
Old 31st July 2012, 16:00   #16
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Quote:
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.
MSG is offline   Reply With Quote
Old 1st August 2012, 11:09   #17
v47
Junior Member
 
Join Date: Jul 2012
Posts: 20
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.
v47 is offline   Reply With Quote
Old 28th October 2012, 04:13   #18
PoRtAbLe_StEaLtH
Junior Member
 
Join Date: Jul 2011
Posts: 49
Thank You

Quote:
Originally Posted by Afrow UK View Post
Or you can use this code:
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?
PoRtAbLe_StEaLtH is offline   Reply With Quote
Old 28th October 2012, 05:14   #19
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Simply add a counter to the loop. Start at zero before ${Do}, then +1 just before you ${Loop}
MSG is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump