- NSIS Discussion
- How to parse a string from a txt file?
Archive: How to parse a string from a txt file?
sckoobs
20th November 2008 19:48 UTC
How to parse a string from a txt file?
Hi there, I'm on my second day of scripting with NSIS and have a need to be able to ready in a value from a text file.
The file format is as follows:
ProductName = Tools
ProductVersion = 6.0.2.0
FileVersion = 6, 0, 2, 0
>
Does anyone know of a reliable and easy way to get the values from this file? I've looked at the ConfigRead function but I haven't been able to get it work at all.
Also, hope I'm not clouding the issue here but, does FileOpen accept relative paths?
Any help is much appreciated.
Thanks,
Andy
Afrow UK
20th November 2008 23:43 UTC
ConfigRead
I would refrain from using relative paths in NSIS scripts in case you change the working directory with SetOutPath.
Stu
LoRd_MuldeR
21st November 2008 02:52 UTC
The easiest method might be using the INI functions:
Given the following file:
[Info]
ProductName = Tools
ProductVersion = 6.0.2.0
FileVersion = 6, 0, 2, 0
You can read the info like this:
Section
ReadINIStr $0 "$INSTDIR\foobar.ini" "Info" "ProductName"
ReadINIStr $1 "$INSTDIR\foobar.ini" "Info" "ProductVersion"
ReadINIStr $2 "$INSTDIR\foobar.ini" "Info" "FileVersion"
SectionEnd
Afrow UK
21st November 2008 16:01 UTC
He didn't include a section header in his snippet in which case ReadINIStr would not work.
Stu
Afrow UK
21st November 2008 16:03 UTC
In fact as you have rightly said ConfigRead won't work here either due to the padding. You will have to use FileRead in a loop with some string manipulation in there to find and chop the bit you want.
Stu
LoRd_MuldeR
21st November 2008 17:20 UTC
Or add one single line (section header) to file ;)
Comperio
21st November 2008 19:52 UTC
This might help. It uses ConfigRead and WordReplace, both of which are in NSIS help. See the attachment for complete example.
${ConfigRead} "$EXEDIR\test.ini" "ProductName" $1
; remove the "=" sign
${WordReplace} "$1" "=" "" "+" $1
; remove extra tab characters:
${WordReplace} "$1" "$\t" "" "{}" $1
; remove extra white space:
${WordReplace} "$1" " " "" "{}" $1
Afrow UK
21st November 2008 20:39 UTC
That will work assuming there are no entries like ProductNameXYZ = thus reading the wrong value.
Stu
Senhu
23rd February 2009 18:16 UTC
Parsing using biterscripting when name=value pairs have flexible syntax
Excellent help so far.
Winamp is great. Occasionally I tend to use biterscripting (.com for free download) for simplified parsing.
The following code in biterscripting will get you the values from the file in the flexible syntax you specified.
# START CODE
var str input, line ; cat <file> $input
while ($input <> "")
do
lex -e "1" $input > $line
wex "[3" $line
done
# END CODE
You may want to put this code in a file to create a script, and pass the input file name ($file) as input argument to that script.
The lex command (line extractor), extracts each line.
The wex command (word extractor), extracts [3, that is everything after (and including) the third word on that line. The wex command takes care of variable sytaxes such as name=value, name = value, name \t=value, etc. The system variable $wsep (word separator) specifies what are considered word separators.
Hope this helps.
Sen