replace text in file - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

ojii
Posts: 5
Joined: Wed Feb 28, 2007 1:12 pm

replace text in file

hi

how can i make the installer replacing some expressions within a file (ascii text file, but not a .txt file) according to textboxes in the installer?

for instance I've got this file example.whatever and in this file is somewhere the text "this_should_be_replaced" and then in the insaller there is a textbox where i can enter something for instance "this_is_an_exemple" and then it replaces every expression in the file that is equal "this_should_be_replaced" with "this_is_an_exemple".

is this possible and if yes: HOW?

looking forward to your answers

Ojii
Phobos
Posts: 85
Joined: Fri Sep 08, 2006 11:52 am
Location: Germany

Hi ojii,

You should be able to do this using a VB script custom action. Here is an example, NEWTEXT is the property of the textbox:

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("NEWTEXT")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "yourfile.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "this_should_be_replaced", NEWTEXT)
Set objFile = objFSO.OpenTextFile(APPDIR + "yourfile.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function
ojii
Posts: 5
Joined: Wed Feb 28, 2007 1:12 pm

hi

thanks for that, but somehow it seems not to be working...

I've added five scripts to change some things in a file to the custom actions (commit and embed) and they look like this:

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("RCON")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "__rconpw__", RCON)
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("NICKONE")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "__nick1__", NICKONE)
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("NICKTWO")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "__nick2__", NICKTWO)
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("NICKTHREE")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "__nick3__", NICKTHREE)
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("NICKFOUR")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "__nick4__", NICKFOUR)
Set objFile = objFSO.OpenTextFile(APPDIR + "Teammenu.res", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function
and the log looks like this: http://gk.tcleimbach.ch/log.txt
Phobos
Posts: 85
Joined: Fri Sep 08, 2006 11:52 am
Location: Germany

I usually run my scripts at "InstallFinalize", maybe you should try that. And there seems to be a mistake in your scripts. Your first script e.g., is RCON the property of the textbox in your installer?
You need to change following line

Code: Select all

strNewText = Replace(strText, "__rconpw__", RCON) 
to

Code: Select all

strNewText = Replace(strText, "__rconpw__", NEWTEXT) 
ojii
Posts: 5
Joined: Wed Feb 28, 2007 1:12 pm

Phobos wrote:I usually run my scripts at "InstallFinalize", maybe you should try that. And there seems to be a mistake in your scripts. Your first script e.g., is RCON the property of the textbox in your installer?
You need to change following line

Code: Select all

strNewText = Replace(strText, "__rconpw__", RCON) 
to

Code: Select all

strNewText = Replace(strText, "__rconpw__", NEWTEXT) 
I've changed that part in all the vbs scripts... still doesnt work...

here you can have a rar archive of all the files (of course you need to update the file sources in the aip file) http://gk.tcleimbach.ch/teammenu.rar
Phobos
Posts: 85
Joined: Fri Sep 08, 2006 11:52 am
Location: Germany

Hi,

I suggest you move your custom actions to the "InstallFinalize" section. To do this rightclick "InstallExecuteSequence" and select "Show Standard Action -> Before Finalization -> InstallFinalize"

Also you need to change the Function Name for your custom actions. It has to be REPLACETEXT as this is the function you use in your scripts.
Phobos
Posts: 85
Joined: Fri Sep 08, 2006 11:52 am
Location: Germany

And you need to specify the correct path to the file you want to edit, change your scripts to look like this:

Code: Select all

Function REPLACETEXT()

Const ForReading = 1
Const ForWriting = 2

APPDIR = Session.Property("APPDIR")
NEWTEXT = Session.Property("RCON")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(APPDIR + "\resource\UI\Teammenu.res", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "__rconpw__", NEWTEXT)
Set objFile = objFSO.OpenTextFile(APPDIR + "\resource\UI\Teammenu.res", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function
Works fine for me.
ojii
Posts: 5
Joined: Wed Feb 28, 2007 1:12 pm

I've done both... changed the section, changed the scripts (adjusted filepath) and changed the scriptnames... still wont work...

I've uploaded the changed files... http://gk.tcleimbach.ch/teammenu.rar
Phobos
Posts: 85
Joined: Fri Sep 08, 2006 11:52 am
Location: Germany

Hm, I don't know why it's failing for you. However, I got it working yesterday, maybe I just send you the modified project?
Phobos
Posts: 85
Joined: Fri Sep 08, 2006 11:52 am
Location: Germany

Hey ojii,

I found your mistake. Look at your "rconreplace.vbs" custom action. You mistyped the Function Name. You entered RCONCEPLACE instead of RCONREPLACE.
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi ojii,

I analyze your project and I notice that you use the result of a folder search as "Application Folder". But if that folder does not exist on the target computer then you need to inform the user about this. This can be done with a launch condition in the "Prerequisites" page:
http://www.advancedinstaller.com/user-g ... tions.html

Regarding the script to find and replace a text in a file you don't need to use 5 custom actions I would suggest to pass the text to replace and the Edit Property as parameters for a function like this:

Code: Select all

Function FindAndReplace(textToReplace, editProp)

Const ForReading = 1
Const ForWriting = 2

pathToFile = Session.Property("APPDIR") + "resource\UI\Teammenu.res"
replaceWith = Session.Property(editProp)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(pathToFile, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, textToReplace, replaceWith)
Set objFile = objFSO.OpenTextFile(pathToFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function

FindAndReplace "__nick1__", "NICKONE"
.
.
.
FindAndReplace "__nick4__", "NICKFOUR"
Copy this code to a .vbs file and use it as "Attached Custom Action" under "InstallFinalize". Let the "Function Name" field empty.

I test this code and works ok. Let us know if you encounter any problems.

Regards,
Gigi
______________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com
ojii
Posts: 5
Joined: Wed Feb 28, 2007 1:12 pm

okay now you'll probably not believe it but i managed to still get the error :(

the vbs file is now:

Code: Select all

Function FindAndReplace(textToReplace, editProp)

Const ForReading = 1
Const ForWriting = 2

pathToFile = Session.Property("APPDIR") + "resource\UI\Teammenu.res"
replaceWith = Session.Property(editProp)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(pathToFile, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, textToReplace, replaceWith)
Set objFile = objFSO.OpenTextFile(pathToFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close

End Function

FindAndReplace "__rconpw__", "RCON"
FindAndReplace "__nick1__", "NICKONE"
FindAndReplace "__nick2__", "NICKTWO"
FindAndReplace "__nick3__", "NICKTHREE"
FindAndReplace "__nick4__", "NICKFOUR"
and it's on finalizeinstall...

still won't work :(:(:(

i guess it really would be the best if you sent me the changed project so i can have a look at it and learn from it.

Oh and I get your point with the folder, thing is I didn't get what I shall do exactly and another thing is that there's no point installing this little thing if you do not have such a folder...

Greez Ojii

PS: I've uploaded my project file with all files needed again:

http://gk.tcleimbach.ch/teammenures.rar

PPS: plz send me the changed file per mail to ojii@freenet.de or just upload it somewhere, thanks a lot for your friendly help

PPPS: I would welcome it very much if you guyz could also tell me what exactly you changed so I don't have to look through all the project :D
Jebrew
Posts: 53
Joined: Thu Jan 18, 2007 8:53 pm

I've got to read/write files in my installer as well. I just use a c++ dll for it. It's pretty easy to do, just be sure you export your functions in the dll and use the correct heading.
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi Ojii,

I analyze your project and I notice that you don't replace the old custom actions with the new one. So what you need to do is to remove old custom actions (nickfour.vbs, nickthree.vbs, etc) with the new one.

Under "InstallFinalize" choose "New Attached Custom Action" and browse for the .vbs file (replace.vbs I see in your archive). Leave the "Function Name" field empty and use (Not Installed) as execution condition.

However if you still encounter problems please contact me on support at advancedinstaller dot com and I'll send you the test project.

Regards,
Gigi
______________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com

Return to “Common Problems”