Sign in to follow this  
Followers 0
ea9000

replace values of an ini file with a loop

8 posts in this topic

want to ask if there is a way to use a loop that can read from one ini file segment ( new values maybe added later) and do a general replace in a file?

an example:

[Environment_Variables]

value_1=a

value_2=b

value_3=c

value_3=d

.

.

value_n=xxx (maybe added later)

file to be replaced is like this

Aa=$value_1

Bb=$value_2

Cc=$value_3

Dd=$value_4

.

.

Nn=value_n

currently i am using this to do the replace line by line which make it hard to maintain within time

FileCopy("config.ini", "temp_config.ini")

FileSetAttrib("temp_config.ini", "-R+A")

$szFile = "temp_config.ini"

$value_1 = IniRead("temp_config.ini", "Environment_Variables", "value_1", "NotFound")

$value_2 = IniRead("temp_config.ini", "Environment_Variables", "$value_2", "NotFound")

$szText = FileRead($szFile, FileGetSize($szFile))

$szText = StringReplace($szText, "$value_1", $value_1)

$szText = StringReplace($szText, "$value_2", $value_2)

thanx in advance for any replay

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm using _FileWriteToLine for something similar...

_FileWriteToLine(@ScriptDir & "/config.ini", 9, $value_1, 1)

You tell it Dir/file

Which line to write to, in this case line 9

Then the value

Then whether to overwrite the line, or append at end, 1 in this case overwrites.

Check out _FileWriteToLine in the help file.

Edited by Werty

Share this post


Link to post
Share on other sites

but this does not detect new lines added to the ini segment ,I don't want to change the code by adding new "updateline" if I add a new value to ini segment. want to have the code once compiled never changed , only the ini file and the file to be update can be maintained later.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If you do know all values of the ini why not delete and recreate the ini at all?

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

I am using one config.ini file which contains ,in segment where I put the whole environment variables , in the other segments I put string values of the first segment like this as an example

config.ini

[environment]

plant_code=1234

[create_dir]

dir_1=c:\$plant_code

then I copy this config.ini file into a temp.ini and replace "dir_1=c:\$plant_code" with "dir_1=c:\1234" ,but as I explained ,the replace code does not need to be changed ,only config.ini is updated ,and code still does the job by looking all values [environment] and replace then in temp.ini

had a good example of using loop to create a set of directories by reading the ini file.

http://www.autoitscript.com/forum/index.php?showtopic=118081

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

So,

you got an ini file which is neutralized (filled with variables) and then you deneutralize the file by doing some

replace $a with xyz

replace $b with 123

and so on?

That's all?

Mega

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

yes the variables are fetched form on segment to replace variables in other segments ,I am trying to build a deployment tool ,each new deployment has some variables changed

cheers

Share this post


Link to post
Share on other sites

Hi,

something easy like this?

$s = "This is your ini file read by FileRead $c is $b + $a"

$before = StringSplit('$a,$b,$c', ',', 2)
$after = StringSplit('1,2,3', ',', 2)

For $i = 0 To UBound($after) -1
    $s = StringReplace($s, $before[$i], $after[$i])
Next
ConsoleWrite($s & @CRLF)

Besides ANT is great for deployment.

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0