ahmetpi 0 Posted December 9, 2020 Hi, I have an ini file like the one below How can I delete keys that have no value in this file? (08:30 and 08:50 in the example) Thanks example ini [MONDAY] 08:00=example1 08:20=example2 08:30= 08:40=example3 08:50=example4 08:50= Share this post Link to post Share on other sites
Danp2 910 Posted December 9, 2020 Read in the section with IniReadSection. Modify the resulting array to remove the desired entries. Use IniWriteSection to output the modified array, thus overwriting the prior section 1 ahmetpi reacted to this [UDF] WebDriver Latest version Wiki FAQs Share this post Link to post Share on other sites
JockoDundee 160 Posted December 9, 2020 (edited) @ahmetpi, btw, your example has duplicate keys: 08:50=example4 08:50= This should never happen if using only the INI functions. I assume it’s just because of a hastily thrown together example, but thought I would point it out in case it’s not. Edited December 9, 2020 by JockoDundee Code hard, but don’t hard code... Share this post Link to post Share on other sites
Nine 996 Posted December 9, 2020 19 minutes ago, JockoDundee said: This should never happen if using only the INI functions. Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = "Test.ini" ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3. Local $aSection[4][2] = [[3, ""], ["Test", "AutoIt"], ["Test", @AutoItVersion], ["Test", @OSVersion]] ; Write the array to the section labelled 'General'. IniWriteSection($sFilePath, "General", $aSection) EndFunc ;==>Example 3 JockoDundee, ahmetpi and mikell reacted to this Not much of a signature but working on it... Spoiler Block all input without UAC Save/Retrieve Images to/from Text Tool to search content in au3 files Date Range Picker Sudoku Game 2020 Overlapped Named Pipe IPC x64 Bitwise Operations Multi-keyboards HotKeySet Fast and simple WCD IPC Multiple Folders Selector GIF Animation (cached) Share this post Link to post Share on other sites
mikell 1,026 Posted December 9, 2020 To answer the OP's question, this could work $txt = StringRegExpReplace(FileRead("example.ini"), '(?m)^[^=]+=\h*$\R?', "") Msgbox(0,"", $txt) 1 ahmetpi reacted to this Share this post Link to post Share on other sites
JockoDundee 160 Posted December 9, 2020 1 hour ago, Nine said: Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = "Test.ini" ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3. Local $aSection[4][2] = [[3, ""], ["Test", "AutoIt"], ["Test", @AutoItVersion], ["Test", @OSVersion]] ; Write the array to the section labelled 'General'. IniWriteSection($sFilePath, "General", $aSection) EndFunc ;==>Example I stand corrected, its absolutely possible. Thanks. 1 ahmetpi reacted to this Code hard, but don’t hard code... Share this post Link to post Share on other sites
ahmetpi 0 Posted December 10, 2020 12 hours ago, mikell said: To answer the OP's question, this could work $txt = StringRegExpReplace(FileRead("example.ini"), '(?m)^[^=]+=\h*$\R?', "") Msgbox(0,"", $txt) its worked, thanks!!! Share this post Link to post Share on other sites
ahmetpi 0 Posted December 10, 2020 13 hours ago, JockoDundee said: @ahmetpi, btw, your example has duplicate keys: 08:50=example4 08:50= This should never happen if using only the INI functions. I assume it’s just because of a hastily thrown together example, but thought I would point it out in case it’s not. I wrote it twice by mistake sorry Share this post Link to post Share on other sites
rudi 32 Posted January 8 @JockoDundee Duplicate entries are possible, but not recommended and can lead to unpredictable results. There are programs, that will take for duplicate keys the first value when reading the ini, others will take the last one. And Autoit is behaving differnt for "duplicates already exist" and "no duplicates so far". Basically: The results are hardly predictable, and duplicates for most purposes should be avoided IMHO. Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = "Test.ini" ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3. Local $aSection[4][2] = [[3, ""], ["Test", "AutoIt"], ["Test", @AutoItVersion], ["Test", @OSVersion]] ; Write the array to the section labelled 'General'. IniWriteSection($sFilePath, "General", $aSection) ShellExecute($sFilePath) MsgBox(0, '', "waiting") for $i = 1 to $aSection[0][0] IniWrite($sFilePath,"General",$aSection[$i][0],$aSection[$i][1]) Next ShellExecute($sFilePath) MsgBox(0, '', "Written ""over"" results in just the first occurence is written to three times") IniDelete($sFilePath,"General") for $i = 1 to $aSection[0][0] IniWrite($sFilePath,"General",$aSection[$i][0],$aSection[$i][1]) Next ShellExecute($sFilePath) EndFunc ;==>Example Earth is flat, pigs can fly, and Nuclear Power is SAFE! Share this post Link to post Share on other sites
JockoDundee 160 Posted January 8 2 hours ago, rudi said: Duplicate entries are possible, but not recommended and can lead to unpredictable results. Yes, I agree. I would have said as much earlier, but I was so mortified by my inaccurate statement that I thought it best just to admit I was wrong, instead of seeming to say “Yes, but...” Code hard, but don’t hard code... Share this post Link to post Share on other sites