KickStarter15 Posted December 12, 2018 Share Posted December 12, 2018 Hi Experts, Good day to all!!! I have this little problem about insertion of new line or let's say a new configuration to my text.ini file. I can only insert to a selected section from my text.ini file (using the below code) but I don't know how to insert my configuration to three or more sections in my text.ini file. Here's the code (from search engine): #include<Array.au3> #include<File.au3> _insertLineToFile(@ScriptDir & '\Text.ini', '[FirstSection]', 'Test to insert here') Func _insertLineToFile($filePath, $after, $insertText) Local $lines If Not _FileReadToArray($filePath, $lines) Then Return -1 _ArrayInsert($lines, _ArraySearch($lines, $after, 1, 0, 0) + 1, $insertText) If _FileWriteFromArray($filePath, $lines, 1) Then Return 1 Return -2 EndFunc ;==>_insertLineToFile Here's the text.ini file format: [FirstSection] Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 [SecondSection] Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 [ThirdSection] Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 [ForthSection] Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 And I want to insert new line like this: Note that the insertion will depend on the declared section where to insert. [FirstSection] Test to insert here ; this is the new line to be inserted Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 [SecondSection] Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 [ThirdSection] Test to insert here ; this is the new line to be inserted Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 [ForthSection] Test to insert here ; this is the new line to be inserted Configuration = 1 Configuration = 2 Configuration = 3 Configuration = 4 Thanks in advance Experts! please let me know if you have clarifications or something. KS15 Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
Subz Posted December 12, 2018 Share Posted December 12, 2018 (edited) Why not use IniWrite($filePath, "Section Name", "Key", "Value")? Sorry just re-read your post, you're not using proper ini format. Edited December 12, 2018 by Subz Link to comment Share on other sites More sharing options...
KickStarter15 Posted December 12, 2018 Author Share Posted December 12, 2018 What do you mean @Subz. Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
Nine Posted December 12, 2018 Share Posted December 12, 2018 you cannot have the same key "configuration" multiple time in the same section ! “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...
Subz Posted December 12, 2018 Share Posted December 12, 2018 As @Nine mentioned your keys aren't unique so the only way to read that data using ini functions would be IniReadSection, since IniRead will only return the last key, value. Here is a couple of examples of how to do to do it with Ini functions, I prefer the first one with the exception it will add an "=" after the data, I just filled it with "=>" #include <Array.au3> Local $sFilePath = @ScriptDir & "\data.ini" Local $aSectionNames = IniReadSectionNames($sFilePath) If @error Then Exit _Example1("; <==this is the new line to be inserted|=>", 1) _Example2("; this is the new line to be inserted") ;~ Example 1 Func _Example1($_sComment, $_iComment = 1) Local $aSection, $iComment For $i = 1 To $aSectionNames[0] $aSection = IniReadSection($sFilePath, $aSectionNames[$i]) $iComment = $_iComment < 1 Or $_iComment > $aSection[0][0] ? 1 : $_iComment _ArrayInsert($aSection, $iComment, $_sComment) IniWriteSection($sFilePath, $aSectionNames[$i], $aSection) Next EndFunc ;~ Example 2 Func _Example2($_sComment) Local $aSection, $sSection = "" For $i = 1 To $aSectionNames[0] $aSection = IniReadSection($sFilePath, $aSectionNames[$i]) $sSection &= "[" & $aSectionNames[$i] & "]" & @CRLF & $_sComment & @CRLF & _ArrayToString($aSection, " = ", 1, -1, @CRLF) & @CRLF Next MsgBox(4096, "Ini File Data", $sSection) EndFunc Link to comment Share on other sites More sharing options...
KickStarter15 Posted December 12, 2018 Author Share Posted December 12, 2018 @Nine and @Subz, sorry if it make's you confused. It's just my sample to specify that under each section it has its own configurations. Like this: this is the format that I have actually, the post is just an example to make it quick. sorry! [FirstSection] AutoInsertBasedOnCategory = true DeletingSectionPart = true AppendTextInLine = true RemoveSpacing = false ... more configurations... [SecondSection] CleanUpFile = true HideInvalidChar = false ChangeSentenceCase = true ChangeTitleCase = fale ... more configurations... . . . and so on....!!! Anways, Subz I'm having this error from your code. Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
Subz Posted December 12, 2018 Share Posted December 12, 2018 How large is the file? If its greater than 32kb it would fail, if its under 32kb than check $aSection @error. Link to comment Share on other sites More sharing options...
KickStarter15 Posted December 12, 2018 Author Share Posted December 12, 2018 As of the current size it has 2MB and this will continue to increase depending on how many configurations to be added weekly or daily. Also, the code you gave will insert to all sections in my text.ini, it should be based on what was declared to be inserted, like I need to insert only to three sections "Frist", "Forth" and "Six"... something like that... Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
Subz Posted December 12, 2018 Share Posted December 12, 2018 Actually just did some testing on my system and the 32kb issue didn't appear to make a difference, I had a 7000 sections with 20 items each 2.3mb file but the code above worked fine for me, I had issues previously with Windows 7 but Windows 10 seems to handle that limitation. Anyway here are updated examples: #include <Array.au3> #include <File.au3> Local $sFilePath = @ScriptDir & "\data.ini" Local $aSectionNames = IniReadSectionNames($sFilePath) If @error Then Exit _Example1("Section1", "; <== Example 1 this is the new line to be inserted |=>", 1) _Example1("Section4", "; <== Example 1 this is the new line to be inserted |=>", 1) _Example1("Section6", "; <== Example 1 this is the new line to be inserted |=>", 1) _Example2("Section2", "; Example 2 this is the new line to be inserted") _Example2("Section3", "; Example 2 this is the new line to be inserted") _Example2("Section7", "; Example 2 this is the new line to be inserted") ;~ Example 1 Func _Example1($_sSectionName, $_sComment, $_iInsert = 1) Local $iInsert Local $aSection = IniReadSection($sFilePath, $_sSectionName) If @error Then Return Local $iInsert = $_iInsert < 1 Or $_iInsert > $aSection[0][0] ? 1 : $_iInsert _ArrayInsert($aSection, $iInsert, $_sComment) IniWriteSection($sFilePath, $_sSectionName, $aSection) EndFunc ;~ Example 2 Func _Example2($_sSectionName, $_sComment) Local $aSection _FileReadToArray($sFilePath, $aSection, $FRTA_COUNT) For $i = $aSection[0] To 1 Step - 1 If $aSection[$i] = "[" & $_sSectionName & "]" Then _FileWriteToLine($sFilePath, $i + 1, $_sComment) Next EndFunc KickStarter15 1 Link to comment Share on other sites More sharing options...
KickStarter15 Posted December 12, 2018 Author Share Posted December 12, 2018 Thanks @Subz, that works perfectly and BTW, I have Windows7 so maybe the error occurred. Well now everything is fine and working as expected. Thank you so much subz. Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
BrewManNH Posted December 12, 2018 Share Posted December 12, 2018 8 hours ago, Subz said: Actually just did some testing on my system and the 32kb issue didn't appear to make a difference, That only affects it when you use IniReadSection, the whole INI file can be larger, but not a single section. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
mikell Posted December 12, 2018 Share Posted December 12, 2018 A bit late but FYI this could also be done using ... guess what ? $txt = "[FirstSection]" & @crlf & _ "AutoInsertBasedOnCategory = true" & @crlf & _ "DeletingSectionPart = true" & @crlf & _ "AppendTextInLine = true" & @crlf & _ "RemoveSpacing = false" & @crlf & _ @crlf & _ "[SecondSection]" & @crlf & _ "CleanUpFile = true" & @crlf & _ "HideInvalidChar = false" & @crlf & _ "ChangeSentenceCase = true" & @crlf & _ "ChangeTitleCase = fale" Msgbox(0,"", $txt) $txt = _Example3($txt, "SecondSection", "; <== Example 3 this is the new line to be inserted =>") Msgbox(0,"", $txt) Func _Example3($txt, $section, $comment) Return StringRegExpReplace($txt, '\[' & $section & '\]\h*\R\K', $comment & @crlf) EndFunc FrancescoDiMuro and caramen 2 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted December 12, 2018 Share Posted December 12, 2018 @mikell That "guess what" Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
KickStarter15 Posted December 13, 2018 Author Share Posted December 13, 2018 @mikell, Thanks, it's also working as expected. Thanks a lot guyz.... Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
caramen Posted December 13, 2018 Share Posted December 13, 2018 12 hours ago, mikell said: guess what ? My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki 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