Archive: Adding lines after a specific string in an ini


Adding lines after a specific string in an ini
I realize that other people have asked similar questions, but I'm finding I'm having a very hard time wrapping my head around this one. I can figure out everything else I need to do for my installer, but this one has left me in the dark.

Also, I know that the standard reply is:

Use FileOpen, FileWrite, FileRead and FileClose. See the Archive for examples.
But I'm having some troubles with that:(

I should also note that the files I'm working on is not a REAL .ini, the game (DeusEx) just has it named that way, but .ini commands such as WriteINIStr still work:)

So about halfway down in the files there is the following:

[Core.System]
PurgeCacheDays=30
SavePath=..\Save
CachePath=..\Cache
CacheExt=.uxx
Paths=..\TNM\Music\*.u
Paths=..\TNM\Maps\*.dx
Paths=..\TNM\Textures\*.utx
Paths=..\TNM\Sounds\*.uax
Paths=..\TNM\Music\*.umx

And I want to modify it so that I have this:

[Core.System]
PurgeCacheDays=30
SavePath=..\Save
CachePath=..\Cache
CacheExt=.uxx
Paths=..\Music\*.u
Paths=..\Maps\*.dx
Paths=..\Textures\*.utx
Paths=..\Sounds\*.uax
Paths=..\Music\*.umx


Any help would be greatly appreciated:)

There are many string manipulation functions on the NSIS Archive:
http://nsis.sourceforge.net/archive/...instances=0,11

You will need to create a new function basing it upon them.
I'd write you one myself, but I cannot currently.

-Stu


You can use the one that replaces strings in the file and replace "Paths=..\TNM\" with "Paths=..\".


Hmm, looking at my post, I can see I was a little more tired than I thought. I actually have to ADD allof the TNM lines, so it would look like so:

[Core.System]
PurgeCacheDays=30
SavePath=..\Save
CachePath=..\Cache
CacheExt=.uxx
Paths=..\TNM\Music\*.u
Paths=..\TNM\Maps\*.dx
Paths=..\TNM\Textures\*.utx
Paths=..\TNM\Sounds\*.uax
Paths=..\TNM\Music\*.umx
Paths=..\Music\*.u
Paths=..\Maps\*.dx
Paths=..\Textures\*.utx
Paths=..\Sounds\*.uax
Paths=..\Music\*.umx


I tried the string replacing bit, but it overwrites the old ones with my new ones (which it's obviously supposed to do:p) So I'll check out that link right now.


INI files should not have duplicate names, add numbers or something else.


Ya, as I said, it's a core game file, so really, the programmers just decided to give it a .ini extention. In actual fact, it's just a text file they renamed to make it seem more important.

I looked into http://nsis.sourceforge.net/archive...&instances=0,11 which does search for strings in a file, but it also loops all the way through. What I want to do it run a search loop until it find a certain line, and then insert a new line (so I dont' overwrite the lines following it) and write in my new paths.


I think the easier method is to read every line of the file, search for the line and then write everything back, including the new line.


Originally posted by Joost Verburg
I think the easier method is to read every line of the file, search for the line and then write everything back, including the new line.
Eeep, this is starting to sound quite complicated:eek:

It's not that difficult. Use the NSIS file commands to read the lines and then write everything back to the file. When writing back, write 2 lines when you found a certain line.


Originally posted by Joost Verburg
It's not that difficult. Use the NSIS file commands to read the lines and then write everything back to the file. When writing back, write 2 lines when you found a certain line.
I'll...try. My NSIS skills are somewhat on the noobish side, but we'll see.

Bah, I have no clue:(

Well, I've been poking around the archives, and I found a script to replace a line of text in the file. My thinking is that I can modify it to search out the line before the place where I want to add in my extra stuff, and then have it replace that line with itself, and my extra lines, like this:

CacheExt=.uxx <--Searches for this line
Paths=..\System\*.u
Paths=..\Textures\*.utx
Paths=..\Sounds\*.uax
Paths=..\Music\*.umx


And I have it replace that line with:

CacheExt=.uxx
Paths=..\TNM\System\*.u
Paths=..\TNM\Maps\*.dx
Paths=..\TNM\Textures\*.utx
Paths=..\TNM\Music\*.umx
Paths=..\TNM\Sounds\*.umx

Paths=..\System\*.u
Paths=..\Textures\*.utx
Paths=..\Sounds\*.uax
Paths=..\Music\*.umx

I tried modifying the script that I found to do what I want. I really don't need it to loop, as there is only one occurance of "CacheExt=.uxx" in the file, but to be honest, I couldn't figure out which part of the loop I needed to keep. I would greatly appreciate it if some kind soul could have a look at my code and point out my errors.

Cheers!


That script has a few errors:

1. If you want to stretch an instruction line on more than one line you should put a backslash at the end of the line. Not closing the quotes is not enough.

2. You are both writing and reading to the same file. When writing to a file it doesn't push the next lines to the bottom, it just overrides them. You should read from one file and write to another. Once that is done, you copy the new file over.


Well, thanks to kichik, I'm getting closer:) I found a script in the archives that basically does what I want, but it's leaving me with a blank file, instead of adding the lines to TNM.ini


Almost there, you just mixed up some variables along the way. $6 with $2 to be exact.


Works like a dream!! Thanks so much guys, I appreciate it:D