Archive: find in file


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.


http://nsis.sourceforge.net/Docs/AppendixE.html#E.2



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

@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.


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?

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.



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

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.



...
!insertmacro un.ConfigWrite
...

Section un.Install
${un.ConfigWrite} "$FILE" "$LINE2" "" $0
SectionEnd

thanks again.
you saved me from a lot of trouble.

OJi.


You are welcome :)