Sign in to follow this  
Followers 0
gcue

rename ini KEY not value

31 posts in this topic

theres two ways ive done this...

1. deleting the entry and recreating it (but this moves it out of the order it was in)

2. i used stringregexpreplace as shown below (but it renames ALL instances of "car1=d1231")

$file = "eh.ini"

$string = FileRead($file)
$string = StringRegExpReplace($string, "car1=d1231", "car=d1231")

FileClose(FileOpen($file,2))
FileWrite($file, $string)

ShellExecute($file)

any other creative ways anyone can think of? (where only that one particular KEY is renamed)

Share this post


Link to post
Share on other sites



So you want to rename the INI key not the section correct? What values are know? Filename section and key? Could you give a example of the INI file and what you want done.

Share this post


Link to post
Share on other sites

well its going to be off a user input, but here's an example of one

[LAO_CRMC_ASSETS]

JEM-12=D0078014

So you want to rename the INI key not the section correct? What values are know? Filename section and key? Could you give a example of the INI file and what you want done.

Share this post


Link to post
Share on other sites

theres two ways ive done this...

1. deleting the entry and recreating it (but this moves it out of the order it was in)

2. i used stringregexpreplace as shown below (but it renames ALL instances of "car1=d1231")

$file = "eh.ini"

$string = FileRead($file)
$string = StringRegExpReplace($string, "car1=d1231", "car=d1231")

FileClose(FileOpen($file,2))
FileWrite($file, $string)

ShellExecute($file)

any other creative ways anyone can think of? (where only that one particular KEY is renamed)

An INI file is plain text, just FileRead(), StringReplace(), FileWrite().

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

this should work for you.

IniWrite("eh.ini", "LAO_CRMC_ASSETS", "JEM-12", "D0078014")
Edited by Smiley357

Share this post


Link to post
Share on other sites

right but that will replace all instances

An INI file is plain text, just FileRead(), StringReplace(), FileWrite().

;)

Share this post


Link to post
Share on other sites

hi smiley,

im trying to rename JEM-12...

this should work for you.

IniWrite("eh.ini", "LAO_CRMC_ASSETS", "JEM-12", "D0078014")

Share this post


Link to post
Share on other sites

Maybe this is what you are looking for?

$replaced=IniRead ( "eh.ini", "LAO_CRMC_ASSET", "JEM-12" )
IniWrite("eh.ini", "LAO_CRMC_ASSETS", "New_Key", $replaced)
IniDelete("eh.ini", "LAO_CRMC_ASSET", "JEM-12")

Share this post


Link to post
Share on other sites

Maybe this is what you are looking for?

$replaced=IniRead ( "eh.ini", "LAO_CRMC_ASSET", "JEM-12" )
 IniWrite("eh.ini", "LAO_CRMC_ASSETS", "New_Key", $replaced)
 IniDelete("eh.ini", "LAO_CRMC_ASSET", "JEM-12")
He already said in the first post that this method disrupts the order of the keys.

Share this post


Link to post
Share on other sites

right but that will replace all instances

Only if your string manipulation is that clumsy. If there is more than one instance of the same key, how were you going to distinguish the one you want to rename? By section? That adds one line of StringInStr() to find the right part of the file to edit. StringRegExpReplace() will likely get in done in one line of code.

FileRead()

... do string manipulation

FileWrite()

It's just not that hard.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

@weaponx

No that is incorrect. He was doing a string write and that appends it to the end of the file. This will not change the order since it is writing it to the section before deleting the key and possibly the section. I did find an error in my code when I tested it.

Use this line.

$replaced=IniRead ( "eh.ini", "LAO_CRMC_ASSET", "JEM-12" ,"NotFound" )

Share this post


Link to post
Share on other sites

but it changes the order within the section.

@weaponx

No that is incorrect. He was doing a string write and that appends it to the end of the file. This will not change the order since it is writing it to the section before deleting the key and possibly the section. I did find an error in my code when I tested it.

Use this line.

$replaced=IniRead ( "eh.ini", "LAO_CRMC_ASSET", "JEM-12" ,"NotFound" )

Share this post


Link to post
Share on other sites

Yes you have me there. It will change the order in the section. It looks like you will have to do string manipulation as PsaltyDS suggested or do a iniwrite for the whole section in the order you want.

Just wondering why does order even matter??

Share this post


Link to post
Share on other sites

hehe i guess it is that clumsy.

i didnt know how to handle the renaming within a given section.

Only if your string manipulation is that clumsy. If there is more than one instance of the same key, how were you going to distinguish the one you want to rename? By section? That adds one line of StringInStr() to find the right part of the file to edit. StringRegExpReplace() will likely get in done in one line of code.

FileRead()

... do string manipulation

FileWrite()

It's just not that hard.

;)

Share this post


Link to post
Share on other sites

bc theres a layout thats associated with the entries and it would change the layout. =/

Yes you have me there. It will change the order in the section. It looks like you will have to do string manipulation as PsaltyDS suggested or do a iniwrite for the whole section in the order you want.

Just wondering why does order even matter??

Share this post


Link to post
Share on other sites

Yes you have me there. It will change the order in the section. It looks like you will have to do string manipulation as PsaltyDS suggested or do a iniwrite for the whole section in the order you want.

Just wondering why does order even matter??

Order doesn't really. It can make a difference for the user reading it.

IniReadSection

--------------------------------------------------------------------------------

Reads all key/value pairs from a section in a standard format .ini file.

IniReadSection ( "filename", "section" )

Parameters

filename The filename of the .ini file.

section The section name in the .ini file.

Return Value

Success: Returns a 2 dimensional array where element[n][0] is the key and element[n][1] is the value.

Failure: Sets @error=1 if unable to read the section (The INI file may not exist or the section may not exist)

-------------------------------------------------------------------------------

Loop through and save all the information in a 2-dim array.

Loop through until you find the key named "JEM-12".

Rename the "$string[$I][0]" to the new name.

Good Luck!

Share this post


Link to post
Share on other sites

Demo:

$sIniData = "[Section Name One]" & @CRLF & _
        "one = 1" & @CRLF & _
        "two = 2" & @CRLF & _
        "three = 3" & @CRLF & _
        "[Section Name Two]" & @CRLF & _
        "one = 1" & @CRLF & _
        "two = 2" & @CRLF & _
        "three = 3" & @CRLF & _
        "[Section Name Three]" & @CRLF & _
        "one = 1" & @CRLF & _
        "two = 2" & @CRLF & _
        "three = 3"
MsgBox(64, "Before", $sIniData)

$sSection = "[Section Name Two]"
$sKeySearch = "two"
$sKeyReplace = "KeyTwo"

$iSecLoc = StringInStr($sIniData, $sSection, 0, 1) + StringLen($sSection)
$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 0, 1, $iSecLoc)
$sIniData = StringMid($sIniData, 1, $iKeyLoc - 1) & $sKeyReplace & " = " & _
        StringMid($sIniData, $iKeyLoc + StringLen($sKeySearch & " = "))
MsgBox(64, "After", $sIniData)

StringRegExpReplace() would be geekier, but it doesn't support non-capturing groups as I understand it, which really hurts its utility for things like this.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

i got an error with this line:

$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 0, 1, $iSecLoc)

i changed it to

$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 1, $iSecLoc)

as it need not be case sensitive but im not getting the output doesnt look like what you intended i dont think..

Demo:

$sIniData = "[Section Name One]" & @CRLF & _
        "one = 1" & @CRLF & _
        "two = 2" & @CRLF & _
        "three = 3" & @CRLF & _
        "[Section Name Two]" & @CRLF & _
        "one = 1" & @CRLF & _
        "two = 2" & @CRLF & _
        "three = 3" & @CRLF & _
        "[Section Name Three]" & @CRLF & _
        "one = 1" & @CRLF & _
        "two = 2" & @CRLF & _
        "three = 3"
MsgBox(64, "Before", $sIniData)

$sSection = "[Section Name Two]"
$sKeySearch = "two"
$sKeyReplace = "KeyTwo"

$iSecLoc = StringInStr($sIniData, $sSection, 0, 1) + StringLen($sSection)
$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 0, 1, $iSecLoc)
$sIniData = StringMid($sIniData, 1, $iKeyLoc - 1) & $sKeyReplace & " = " & _
        StringMid($sIniData, $iKeyLoc + StringLen($sKeySearch & " = "))
MsgBox(64, "After", $sIniData)

StringRegExpReplace() would be geekier, but it doesn't support non-capturing groups as I understand it, which really hurts its utility for things like this.

;)

Share this post


Link to post
Share on other sites

i got an error with this line:

$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 0, 1, $iSecLoc)

i changed it to

$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 1, $iSecLoc)

as it need not be case sensitive but im not getting the output doesnt look like what you intended i dont think..

What error? It runs fine as posted, as I tested it before posting it and after seeing your comment.

You broke the parameters by changing it, see the help file on StringInStr():

$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 0, 1, $iSecLoc)

$sIniData is the string to evaluate

$sKeySearch & " = " is the string to search for ("two = ")

0 is case insensitive

1 is first match

$iSecLoc is the start position

What version of AutoIt are you running? You should be at 3.2.12.1.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

using 3.2.10.0

its part of a larger script that works only in this version =/

shame on me for doubting you salty. i did find it strange --

What error? It runs fine as posted, as I tested it before posting it and after seeing your comment.

You broke the parameters by changing it, see the help file on StringInStr():

$iKeyLoc = StringInStr($sIniData, $sKeySearch & " = ", 0, 1, $iSecLoc)

$sIniData is the string to evaluate

$sKeySearch & " = " is the string to search for ("two = ")

0 is case insensitive

1 is first match

$iSecLoc is the start position

What version of AutoIt are you running? You should be at 3.2.12.1.

;)

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