within
3rd October 2011 13:18 UTC
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...
MSG
3rd October 2011 13:58 UTC
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:
within
3rd October 2011 16:02 UTC
Thanks very much MSG for your code!
I'll test it tomorrow.
MSG
3rd October 2011 20:46 UTC
Oh, and you should always FileClose. So you could move that to below the done label, I guess.
within
4th October 2011 09:30 UTC
Indeed, the FileClose was not at the best positon. I had to add anothyer one too, depending on conditions.
here is my 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
4th October 2011 10:35 UTC
tested with success!
Thanks MSG
MSG
4th October 2011 12:49 UTC
Next up: Using LogicLib instead of Strcmp and goto's. :)
within
4th October 2011 14:10 UTC
I didn't know about this one; looks cool. Thanks for the tip!