Jump to content
Sign in to follow this  
AustrianOak

Deleting a specific value in a key

Recommended Posts

AustrianOak

I am trying to delete a certain value in my ini file and I tried IniDelete() and then read throught the helpfile to find out that IniDelete() can delete values but it looks to me as if it deletes all or none.

Is there some way I can delete a certain value in a key?

Maybe I'm confused, maybe I severely misread the helpfile.

Please someone correct me.

Edited by nowagain

Share this post


Link to post
Share on other sites
rudi

I am trying to delete a certain value in my ini file and I tried IniDelete() and then read throught the helpfile to find out that IniDelete() can delete values but it looks to me as if it deletes all or none.

Is there some way I can delete a certain value in a key?

Maybe I'm confused, maybe I severely misread the helpfile.

Please someone correct me.

I'm not sure I get what you are asking for.

Maybe you could give us an example, how your INI looks like originally, and how it shall look like finally?

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
AustrianOak

here's an example of what it looks like:

[section]

key1=value1;value2

I'm running through a for loop in my program and depending on certain results it will delete a specific value in a key.

Let's say I wanted to delete value2 above.

What I wahnt it to look like finally is like this:

[section]

key1=value1

How? IniDelete() doesn't look like it can do it from what I read in the helpfile.

Edited by nowagain

Share this post


Link to post
Share on other sites
ResNullius

here's an example of what it looks like:

[section]

key1=value1;value2

I'm running through a for loop in my program and depending on certain results it will delete a specific value in a key.

Let's say I wanted to delete value2 above.

What I wahnt it to look like finally is like this:

[section]

key1=value1

How? IniDelete() doesn't look like it can do it from what I read in the helpfile.

A standard Ini file wouldn't quite be setup that way for deleting values from. It would be more like

[section]

key1=value1

key2=value2

Which IniDelete() can easily handle.

Do you absolutely have to have the format with more than one value in a single key as you posted?

If so, what you want is doable, it just needs some custom coding with IniRead(), StringReplace(), & IniWrite() (probably)...

Share this post


Link to post
Share on other sites
AustrianOak

Ok, but how do I go about doing that? I have no idea how to implement iniwrite and iniread to delete a specific value...

Share this post


Link to post
Share on other sites
ProgAndy

First you have touse IniRead,

Then StringSplit

Remove the Value from the Array,

use _ArrayToString

And Write it back to the ini


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
Kiti

here's an example of what it looks like:

[section]

key1=value1;value2

I'm running through a for loop in my program and depending on certain results it will delete a specific value in a key.

Let's say I wanted to delete value2 above.

What I wahnt it to look like finally is like this:

[section]

key1=value1

How? IniDelete() doesn't look like it can do it from what I read in the helpfile.

Try this:

$keys = "value1;value2"
If .... Then
    $key = StringTrimRight($keys, 7)
Else
    $key = StringTrimLeft($keys, 7)
EndIf

If the lenght of $keys is variable, then you should use StringSplit, as ProgAndy said:

$keys ="Value1,Value2,Value3,Value4"
$key = StringSplit($keys, ",")

This would create a one dimensional array. Now, $key[1] = "Value1", $Keys[3]="Value3".

I'm not sure if this is helpful to this specific situation, but it's a good thing to know:

When using a for loop and an x-dimensional array, you can do something like this:

for $i=1 to $keys[0]
;do something with $keys[$i]
Next

This will do something to each value of an array. Note: $keys[0] is the dimension of the array, how many values it has stored in. Sorry if you already knew this things. :)

Edited by Kiti

Share this post


Link to post
Share on other sites
SmOke_N

_IniDeleteValue("SomeIni.ini", "Section", "Key1", "myvaluetoreplace", ",")

Func _IniDeleteValue($sIni, $sSection, $sKey, $sFind, $sDelim = "", $vCaseSensitive = False)
    Local $sRead = IniRead($sIni, $sSection, $sKey, "")
    If $sRead = "" Then Return SetError(1, 0, 0)
    
    Local $vCase = "(?i)"
    If $vCaseSensitive Then $vCase = ""
    
    $sRead = StringRegExpReplace($sRead, "(?s)" & $vCase & _
                $sDelim & $sFind & "$|" & $sFind & $sDelim, "")
    
    Return IniWrite($sIni, $sSection, $sKey, $sRead)
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
AustrianOak

Thank you Smoke_N.

and thank you all others who helped me.

Problem resolved.

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  

×