Me again:
Can you look for multiple strcontains in a file?
I've tried a couple of variations, what I want to do is below, but it only ever seems to find the first ${strcontains}, and then skips over the rest.
The file itself has multiple lines. Some of the lines will have the text I am searching for, and some won't.
A typical file will look like this:
STAGES.TXT
Stage1true
Stage2true
pmp123456
EXP000479
cusMarkatSADiE
Stage3true
EOF
iffileexists "$Temp\stages.txt" openfile nofile
openfile:
Fileopen $9 "$Temp\Stages.txt" r
${ForEach} $i 1 $R0 + 1
Fileread $9 $R4 10
pop $R4
${strcontains} $serial "EXP" $R4
STRCMP $serial "" notfound
strcpy $serial $R4
messagebox mb_ok " Serial number is: $serial"
${strcontains} $order "pmp" $R4
STRCMP $order "" notfound
strcpy $order $R4
messagebox mb_ok " Order number is: $order"
${strcontains} $customer "cus" $R4
STRCMP $customer "" notfound
strcpy $customer $R4
messagebox mb_ok " Customer is: $customer"
goto done
notfound:
; I never found nothin'
done:
${Next}
What am I missing?
Can you search for multiple strcontains?
5 posts
And strcontains is a macro you wrote? found somewhere? from the wiki?
If the line always starts with the part of the string you are looking for it is better to just use StrCpy with the maxlen parameter to copy the first 3 characters and then StrCmp that...
If the line always starts with the part of the string you are looking for it is better to just use StrCpy with the maxlen parameter to copy the first 3 characters and then StrCmp that...
Oh Lord, I can barely write my own name, let alone write a macro... It's one I found ages ago http://nsis.sourceforge.net/StrContains
Yes you're right Anders, I wasn't thinking.
I already have the strcontains function loaded to check for the occurance of a vendor Id (To see if a particular USB device is plugged in), so I figured I'd just reuse the function, but it only seems to find the first instance... i.e. it finds the serial number, and ignors the rest.
I'll try it another way. Thanks
Yes you're right Anders, I wasn't thinking.
I already have the strcontains function loaded to check for the occurance of a vendor Id (To see if a particular USB device is plugged in), so I figured I'd just reuse the function, but it only seems to find the first instance... i.e. it finds the serial number, and ignors the rest.
I'll try it another way. Thanks
pop $R4 in your script does not look right to me. Passing 10 as the maxlen also seems strange unless STAGES.TXT does not contain any newlines at all but then "cusMarkatSADiE" would be too long?
If each entry is on their own line I would just do:
If each entry is on their own line I would just do:
!include LogicLib.nsh
Section
InitPluginsDir
FileOpen $0 "$PluginsDir\Stages.txt" w ; Generate example file
FileWrite $0 "Stage1true$\r$\n"
FileWrite $0 "Stage2true$\n" ; Unix newline
FileWrite $0 "pmp123456$\r" ; Mac newline
FileWrite $0 "EXP000479$\r$\n"
FileWrite $0 "$\r$\n" ; a empty line for testing
FileWrite $0 "cusMarkatSADiE$\r$\n"
FileWrite $0 "Stage3true$\r$\n"
FileClose $0
; Test file has been created, will open and parse it now:
FileOpen $0 "$PluginsDir\Stages.txt" r
loop:
FileRead $0 $1
StrCmp $1 "" done
killnewlines:
StrCpy $2 $1 "" -1
StrCmp $2 "$\n" 0 +3
StrCpy $1 "$1" -1
Goto killnewlines
StrCpy $2 $1 "" -1
StrCmp $2 "$\r" 0 +3
StrCpy $1 $1 -1
Goto killnewlines
DetailPrint "Parsing line:|$1|"
StrCpy $2 $1 3 ; Copy the first 3 characters in the line
${If} $2 == "EXP"
StrCpy $3 $1 "" 3
DetailPrint EXP=$3
${ElseIf} $2 == "cus"
StrCpy $3 $1 "" 3
DetailPrint cus=$3
${EndIf}
Goto loop
done:
FileClose $0
SectionEnd Thanks Anders, that makes sense. I should have increased the maxlen. It was initially there when I was on;y looking for stagextrue.
I'll have another go at this when I get back from holiday 🙂 Yay Holiday.
Thanks for your help.
Mark
I'll have another go at this when I get back from holiday 🙂 Yay Holiday.
Thanks for your help.
Mark