Jump to content

Deleting a specific value in a key


Recommended Posts

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
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

_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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...