Archive: Best Practice - Read file, edit contents, write new file


Best Practice - Read file, edit contents, write new file
Hello all,

I am after some best practice advise relating to reading a text file, replacing some information and then writing the data to a new file.

As an example, my source file contains:


com.iplanet.services.debug.level=message
com.iplanet.services.debug.directory=c:/Path/To/Debug

com.iplanet.am.naming.url=https://naming.com/url
com.iplanet.am.naming.ignoreNamingService=true

com.iplanet.am.server.protocol=https
com.iplanet.am.server.host=naming.com
com.iplanet.am.server.port=443

com.iplanet.am.notification.url=http://localserver.com:3700/LocalService/service.svc

com.iplanet.am.cookie.name=iPlanetDirectoryPro
com.iplanet.am.cookie.encode=true

com.iplanet.am.jssproxy.trustAllServerCerts=true

com.iplanet.am.version=6.1

com.iplanet.security.SecureRandomFactoryImpl=com.iplanet.am.util.SecureRandomFactoryImpl
com.iplanet.security.SSLSocketFactoryImpl=netscape.ldap.factory.JSSESocketFactory
com.iplanet.security.encryptor=com.iplanet.services.util.JCEEncryption

com.sun.identity.webcontainer=IAS7.0


I need to change the line:

com.iplanet.services.debug.directory=c:/Path/To/Debug


to be

com.iplanet.services.debug.directory=c:/NewPath/Somewhere/Else


and

com.iplanet.am.notification.url=http://localserver.com:3700/LocalService/service.svc


to

com.iplanet.am.notification.url=http://localserver.com:3700/NewName/service.svc


I originally started of with something like this:


ClearErrors
FileOpen $0 "${sourcefile}" r
FileRead $0 $1
FileClose $0

${WordReplace} $1 "c:/Path/To/Debug" "c:/NewPath/Somewhere/Else" "E+" $R0
${WordReplace} $R0 "/LocalService/" "/NewName/" "E+" $R1

ClearErrors
FileOpen $0 "${targetfile}" w
FileWrite $0 $1
FileClose $0


However this resulted in some weird results (the target file only contained the first line of the source file)

So after hunting around the wiki I haven't found a definitive way of doing this. So can anybody point me in the right direction.

EDIT : After some further hunting, I came across the following:

http://nsis.sourceforge.net/More_adv...e_text_in_file

Which seems to work. All I need to do is copy the source for to the target location and then use these functions to make the edits.

use ReadIniStr to read the path as it will be easier. then use Copy to copy the file(s) to the target location and then use WriteIniStr to change it in the file.


You cannot use ReadINIStr if there are no section headers. Use ConfigWrite instead.

Stu


Originally posted by Afrow UK
You cannot use ReadINIStr if there are no section headers. Use ConfigWrite instead.

Stu
how did i miss that?? yes ConfigWrite is the way to go on this

Originally posted by Afrow UK
You cannot use ReadINIStr if there are no section headers. Use ConfigWrite instead.

Stu
I did try config write however one of the changes I needed to work it would have required some workarounds.

In summary this line in the source file:

[code]
com.iplanet.am.notification.url=http://localserver.com:3700/NewName/service.svc
[code]

The "localserver.com" is unique for every install, so I need to keep that and only change the "/NewName/" part. So I would have had to use configread, then do string replacement, then a config write. Due to this, in the end it was easier to use the item in the edit. Unless of course there is a "gotcha" somewhere that I haven't spotted.

Can you not use ConfigRead + WordReplace + ConfigWrite?

Stu


i have the same problem, i uused configread and configwrite, and he changed the first entry, he fond. but the entry i want to change, can be more then one times in the File. And he should be change every entry.

How can i change the other entrys, or why he change only one entry?


WriteINIStr should be your solution according to your other topic.

Stu


can i use writeinistr with every Filetyp?


ok now i have exactly the same Problem and cant use writeinistr, because i have no [Sections]

and if i use Configwrite, he adds it at the end of the File. but i want to replace a substring


... test@"Database name" ...


i want change the database name.

You have to use StrReplace macro. http://nsis.sourceforge.net/StrReplace_v4


ok i will test it