o_owd
14th September 2005 21:40 UTC
find in file
hello,
one quick question...
i need to find in a text file a specific line. then (after that line) search for another one and if it does not exist add it.
now the question, what function should i use ?
there are a lots in "Text Files Manipulation Functions" in the NSIS Wiki.
thanks.
Instructor
15th September 2005 11:06 UTC
Name "Output"
OutFile "Output.exe"
Var FILE
Var LINE1
Var LINE2
Section
StrCpy $FILE "SCANDISK.LOG"
StrCpy $LINE1 "First Line$\r$\n"
StrCpy $LINE2 "Second Line$\r$\n"
StrCpy $0 0
FileOpen $1 $FILE r
loop:
FileRead $1 $2
IfErrors append
StrCmp $0 1 +4
StrCmp $2 $LINE1 0 loop
StrCpy $0 1
goto loop
StrCmp $2 $LINE2 end loop
append:
StrCmp $0 0 end
FileClose $1
FileOpen $1 $FILE a
FileSeek $1 0 END
FileWrite $1 $LINE2
end:
FileClose $1
SectionEnd
o_owd
17th September 2005 10:25 UTC
@Yathosho - i found those too.
@Instructor - thanks for the script.
it is working, but my line (with your script) is placed at the end of the file.
how to change that ?
i tried to change the script but had no luck.
thanks,
OJi.
Instructor
17th September 2005 12:59 UTC
it is working, but my line (with your script) is placed at the end of the file.
how to change that ?
i tried to change the script but had no luck.
Yes, it is placed at the end of the file. You want to place it just after first founded line?
o_owd
17th September 2005 19:57 UTC
yes, i want to place it after i find the first line.
it is about an entry in system.ini (if this hepls). this is why is not good at the end of the file.
thanks,
OJi.
Instructor
17th September 2005 21:46 UTC
Name "Output"
OutFile "Output.exe"
!include "TextFunc.nsh"
!insertmacro ConfigWrite
!insertmacro TrimNewLines
Var FILE
Var LINE1
Var LINE2
Section
StrCpy $FILE "SCANDISK.LOG"
StrCpy $LINE1 "First Line"
StrCpy $LINE2 "Second Line"
StrCpy $0 0
FileOpen $1 $FILE r
loop:
FileRead $1 $2
IfErrors append
${TrimNewLines} "$2" $2
StrCmp $0 1 +4
StrCmp $2 $LINE1 0 loop
StrCpy $0 1
goto loop
StrCmp $2 $LINE2 0 loop
StrCpy $0 0
append:
FileClose $1
StrCmp $0 0 end
${ConfigWrite} "$FILE" "$LINE1" "$\r$\n$LINE2" $0
end:
SectionEnd
o_owd
18th September 2005 07:21 UTC
thank you very much.
it is working. many many thanks.
one last question - how do i delete the second line ?
when i uninstall i must delete the line.
OJi.
Instructor
18th September 2005 09:15 UTC
...
!insertmacro un.ConfigWrite
...
Section un.Install
${un.ConfigWrite} "$FILE" "$LINE2" "" $0
SectionEnd
o_owd
18th September 2005 09:39 UTC
thanks again.
you saved me from a lot of trouble.
OJi.
Instructor
18th September 2005 09:46 UTC
You are welcome :)