Replacing strings in a textfile

Antworten
Benutzeravatar
SisterOfMercy
Beiträge: 1522
Registriert: 22 Jun 2012, 19:18

Replacing strings in a textfile

Beitrag von SisterOfMercy »

I want to edit a textfile, and replace all occurences of a string. I have something, but saveTextFile doesn't seem to work, and I can't find a working example anywhere. Searching this forum for stringlist and replace is also not very handy.

This is what I have. Because I couldn't find other solutions I made this up. It loads qgis.bat as a textfile. For every line in the textfile the @osgeo4w@ is replaced by $InstallDir64$. Every line is written to a new empty stringlist, and this new stringlist is saved to a textfile.

Code: Alles auswählen

[Actions]
DefVar $InstallDir64$
DefVar $ProductId$

DefStringList $newtextfile$ 
DefStringList $textfile$
DefVar $file$
DefVar $patchfile$
DefVar $testline$

Set $ProductId$ = "qgis"
Set $InstallDir64$ = "%ProgramFiles64Dir%\QGIS"

set $file$ = $InstallDir64$ + "\bin\qgis.bat"
Sub_PatchFile

[Sub_PatchFile]
Set $patchfile$ = $file$
if FileExists($patchfile$)
	set $textfile$ = loadTextFile($patchfile$)
	for $line$ in $textfile$ do Sub_PatchLine
	saveTextFile($newtextfile$,$patchfile$)
endif

[Sub_PatchLine]
set $testline$ = '$line$'
if contains($testline$, "@osgeo4w@")
	set $testline$ = stringReplace($testline$, "@osgeo4w@", $InstallDir64$)
endif
set $newtextfile$ = addtolist($newtextfile$,$testline$)
I do get the new stringlist completed, but savetextfile doesn't work?

Code: Alles auswählen

[2] [aug 16 20:14:09:989]   Syntax Error in Section: Sub_PatchFile (Command in line 403 in section: Sub_PatchFile file: setup3264.opsiscript section start at line: 398; origin: setup3264.opsiscript line: 403): saveTextFile -> undefined
What am I doing wrong with saveTextFile?
Bitte schreiben Sie Deutsch, when I'm responding in the German-speaking part of the forum!
Benutzeravatar
SisterOfMercy
Beiträge: 1522
Registriert: 22 Jun 2012, 19:18

Re: Replacing strings in a textfile

Beitrag von SisterOfMercy »

Code: Alles auswählen

	if (saveTextFile($newtextfile$,$patchfile$))
	endif
Huh? If I do it like this... it works. Why?
Bitte schreiben Sie Deutsch, when I'm responding in the German-speaking part of the forum!
Benutzeravatar
n.wenselowski
Ex-uib-Team
Beiträge: 3194
Registriert: 04 Apr 2013, 12:15

Re: Replacing strings in a textfile

Beitrag von n.wenselowski »

Hi SisterOfMercy,

the function wants to return something and I think this has to do with it.
Does it work if you do somethink like this:

Code: Alles auswählen

$myvar$ = saveTextFile(...)

Kind regards

Niko

Code: Alles auswählen

import OPSI
Benutzeravatar
SisterOfMercy
Beiträge: 1522
Registriert: 22 Jun 2012, 19:18

Re: Replacing strings in a textfile

Beitrag von SisterOfMercy »

n.wenselowski hat geschrieben:the function wants to return something and I think this has to do with it.

Code: Alles auswählen

set $myvar$ = saveTextFile(...)
I have tried it like this and got this error:

Code: Alles auswählen

[6] [aug 17 15:29:33:261]     Set  $flep$ = saveTextFile($newtextfile$,$patchfile$)
[2] [aug 17 15:29:33:280]     Syntax Error in Section: Sub_PatchFile (Command in line 378 in section: Sub_PatchFile file: setup3264.opsiscript section start at line: 372; origin: setup3264.opsiscript line: 378): $flep$ = saveTextFile($newtextfile$,$patchfile$) -> saveTextFile($newtextfile$,$patchfile$) illegal String Expressionstr
Your version without set doesn't work either:

Code: Alles auswählen

[2] [aug 17 15:30:32:612]     Syntax Error in Section: Sub_PatchFile (Command in line 378 in section: Sub_PatchFile file: setup3264.opsiscript section start at line: 372; origin: setup3264.opsiscript line: 378): $flep$ -> undefined
[2] [aug 17 15:30:32:615]     Syntax Error in Section: Sub_PatchFile (Command in line 379 in section: Sub_PatchFile file: setup3264.opsiscript section start at line: 372; origin: setup3264.opsiscript line: 379): EndIf -> expected
Bitte schreiben Sie Deutsch, when I'm responding in the German-speaking part of the forum!
Benutzeravatar
n.wenselowski
Ex-uib-Team
Beiträge: 3194
Registriert: 04 Apr 2013, 12:15

Re: Replacing strings in a textfile

Beitrag von n.wenselowski »

Hi,

so I've looked into opsi-script-test and appearantly all calls are made as part of an if-construct.

I will see that someone with more winst-expertise has a look at this.


Kind regards

Niko

Code: Alles auswählen

import OPSI
Benutzeravatar
d.oertel
uib-Team
Beiträge: 3319
Registriert: 04 Jun 2008, 14:27

Re: Replacing strings in a textfile

Beitrag von d.oertel »

Hi,

savetextfile is a boolean function.
That is described is in the manual.

I have filed a low priority feature request for a void version of savetextfile.

cheers
d.oertel
opsi support - uib gmbh

For productive opsi installations we recommend support contracts.
http://www.uib.de
http://www.opsi.org
Benutzeravatar
SisterOfMercy
Beiträge: 1522
Registriert: 22 Jun 2012, 19:18

Re: Replacing strings in a textfile

Beitrag von SisterOfMercy »

d.oertel hat geschrieben: savetextfile is a boolean function.
That is described is in the manual.
I have filed a low priority feature request for a void version of savetextfile.
Just change the manual. Show a few examples how to use a boolean function. Maybe change something else so I can use set $var$ = savetextfile($file$). One would think you could later check $var$ for true/false.
Bitte schreiben Sie Deutsch, when I'm responding in the German-speaking part of the forum!
Antworten