cheryll
14th October 2009 08:08 UTC
Search for a line, replace or append
Hi,
I'm trying to do something rather specific with my installer right now, and I don't seem to be able to find a macro or code for it.
What I need to do is to search a file for the occurrence of a specific line-beginning. If I find it, I need to replace the full line with a string, if I don't find it, I need to append that string to the file.
I found code snippets for searching for a line- beginning and replacing that line, as well as for appending a line to the file, but I just can't figure out how to piece those together.
I'd be grateful for all help.
cheryll
14th October 2009 09:32 UTC
I hacked something together, but now I'm getting a strange error:
"Processed 1 file, writing output:
Error: could not resolve label "_LogicLib_Label_1" in unnamed install section (0)
Error - aborting creation process"
I have no idea what this could mean, here's my code:
Section
;Wert aus INI auslesen
ReadINIStr $Timeout "Some.ini" "FF" "Value"
${If} Timeout != ""
StrCpy $ReplaceLine ${FindLine} $Timeout
StrCpy $ReplaceLine $ReplaceLine ");"
;Die ersten 10 Profile werden durchsucht
${ForEach} $9 0 10 + 1
StrCpy $1 "Profile"$9
ReadINIStr $2 "${Profile}" "$1" "Default"
;Default-Flag suchen
${If} $2 == "Default"
ReadINIStr $4 "${Profile}" $1 "isRelativ"
ReadINIStr $0 "${Profile}" $1 "Path"
;wird überprüft, ob der Pfad relativ ist, und entsprechend zusammengesetzt
${If} $4 == "1"
StrCpy $Prof_Path ${Profile}$0
${Else}
StrCpy $Prof_Path $0
${EndIf}
${EndIf}
;dannach wird die Schleife abgebrochen (Ergebniss gefunden!)
${Break}
${Next}
;prüft die Länge des Vergleichsstrings
StrLen $R1 ${FindLine}
ClearErrors
FileOpen $0 "$Prof_Path\file" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop:
;Zeile einlesen
FileRead $0 $2
IfErrors append
;schneidet Zeile auf Länge des Vergleichsstring
StrCpy $3 "$2" $R1
;vergleicht Zeile mit Vergleichsstring
StrCmp "$3" ${FindLine} replace notequal
replace:
;wenn Zeile gefunden wurde Zeile ersetzen
StrCpy $2 "$ReplaceLine$\r$\n"
FileWrite $R0 $2
Goto done
notequal:
FileWrite $R0 $2
Goto loop
append:
;wenn Flag nicht gesetzt, Zeile an die Datei anhängen
FileClose $R0
FileOpen $R0 "$Prof_Path\file" a
FileSeek $R0 0 END
FileWrite $R0 "$ReplaceLine$\r$\n"
;Dateien schließen und Ausgangsdatei mit Tempdatei ersetzen
done:
FileClose $0
FileClose $R0
Delete "$Prof_Path\file"
CopyFiles /SILENT $R0 "$Prof_Path\file"
Delete $R0
SectionEnd
MSG
14th October 2009 20:49 UTC
Originally posted by cheryll
I hacked something together, but now I'm getting a strange error:
"Processed 1 file, writing output:
Error: could not resolve label "_LogicLib_Label_1" in unnamed install section (0)
Error - aborting creation process"
I have no idea what this could mean, here's my code:
It means that LogicLib is missing a label somewhere. In this case, you're forgetting to ${EndIf} after your ${Next}.