Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Reply
Thread Tools Search this Thread Display Modes
Old 3rd October 2011, 13:18   #1
within
Junior Member
 
Join Date: Aug 2008
Posts: 19
Question LineFind : discovering an existing line that starts with a dedecated string: HOW?

Hi there,

I'd wish to modify a text file only if a dedicated string in this file is NOT written there already.

I'd like to compare each lines in a file to check if this line starts with a dedicated string, let's say a line can be:
'user is defined as blabla'

and string to use for check is 'user'.

so, as soon as, within a file, the script does find a line that starts with 'user' it will stop and goto another part of the script that will do 'action 1', or if it does not find any line that starts with the string 'user', it will do 'action 2'.

I thought I could use ${LineFind} "[File1]" "[File2|/NUL]" "[LineNumbers]" "Function"

but I have some difficulties for the 'Function', and compare process...
within is offline   Reply With Quote
Old 3rd October 2011, 13:58   #2
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
ClearErrors
FileOpen $0 $INSTDIR\file.dat r
IfErrors notfound
loop:
FileRead $0 $1
IfErrors goto notfound
StrCpy $2 $1 4
StrCmp $2 "user" bingo
goto loop
end:
FileClose $0
notfound:
MessageBox MB_OK "Not found."
goto done
bingo:
MessageBox MB_OK "Found."
goto done
done:
MSG is offline   Reply With Quote
Old 3rd October 2011, 16:02   #3
within
Junior Member
 
Join Date: Aug 2008
Posts: 19
Thanks very much MSG for your code!

I'll test it tomorrow.
within is offline   Reply With Quote
Old 3rd October 2011, 20:46   #4
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Oh, and you should always FileClose. So you could move that to below the done label, I guess.
MSG is offline   Reply With Quote
Old 4th October 2011, 09:30   #5
within
Junior Member
 
Join Date: Aug 2008
Posts: 19
Indeed, the FileClose was not at the best positon. I had to add anothyer one too, depending on conditions.

here is my code:

code:
Section SecSetServ

; check if Environement variable is already defined
ClearErrors
FileOpen $0 "$WINDIR\system32\drivers\etc\services" a ; Open mode is "a" for append
IfErrors notfound
loop:
FileRead $0 $1
IfErrors notfound
StrCpy $2 $1 4
StrCmp $2 "user" found
Goto loop
notfound:
Goto done
found:
Goto finished
done:
FileClose $0
; Modify system files ("service" files)
Call SysFiles
Goto veryend

finished:
FileClose $0
${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6
Push "$2/$1/$0 $4:$5:$6 [INSTALL] - Services ports ALREADY defined.$\r$\n $\r$\n" ;text to write to file
Push "$INSTDIR\_setup.log" ;file to write to
Call WriteToFile

veryend:

SectionEnd

within is offline   Reply With Quote
Old 4th October 2011, 10:35   #6
within
Junior Member
 
Join Date: Aug 2008
Posts: 19
tested with success!

Thanks MSG
within is offline   Reply With Quote
Old 4th October 2011, 12:49   #7
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Next up: Using LogicLib instead of Strcmp and goto's.
MSG is offline   Reply With Quote
Old 4th October 2011, 14:10   #8
within
Junior Member
 
Join Date: Aug 2008
Posts: 19
I didn't know about this one; looks cool. Thanks for the tip!
within 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