ahmetpi Posted December 9, 2020 Share 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= Link to comment Share on other sites More sharing options...
Danp2 Posted December 9, 2020 Share 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 ahmetpi 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
JockoDundee Posted December 9, 2020 Share 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... Link to comment Share on other sites More sharing options...
Nine Posted December 9, 2020 Share 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 mikell, JockoDundee and ahmetpi 3 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted December 9, 2020 Share Posted December 9, 2020 To answer the OP's question, this could work $txt = StringRegExpReplace(FileRead("example.ini"), '(?m)^[^=]+=\h*$\R?', "") Msgbox(0,"", $txt) ahmetpi 1 Link to comment Share on other sites More sharing options...
JockoDundee Posted December 9, 2020 Share 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. ahmetpi 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
ahmetpi Posted December 10, 2020 Author Share 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!!! Link to comment Share on other sites More sharing options...
ahmetpi Posted December 10, 2020 Author Share 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 Link to comment Share on other sites More sharing options...
rudi Posted January 8, 2021 Share Posted January 8, 2021 @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! Link to comment Share on other sites More sharing options...
JockoDundee Posted January 8, 2021 Share Posted January 8, 2021 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... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now