Jump to content

Inserting new line in multiple section heads in text file


Recommended Posts

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.:sweating:

 

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

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

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

image.png.67a5d392f9434cebbdb0b62f352241b6.png

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

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

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

 

Link to comment
Share on other sites

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.:lol:

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

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 Gude
How 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

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

 

Link to comment
Share on other sites

12 hours ago, mikell said:

guess what ?

:drool:

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 - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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