AlienStar Posted August 13, 2020 Share Posted August 13, 2020 (edited) hello everybody I have an ini sections like this: [1] s = 1 m = 4 [2] h = 2 j = 7 k = 9 l = 22 r = 99 [3] a = 0 l = 111 y = 88 w = 90 [4] d =0 how can I delete [2] section (So [3] section become [2] and [4] become [3] ...etc) so ini will be re-sorted after delete a section ?? Edited August 13, 2020 by AlienStar Link to comment Share on other sites More sharing options...
pseakins Posted August 13, 2020 Share Posted August 13, 2020 I would write a couple of loops to iterate through the file using IniReadSectionNames() , IniReadSection(), IniRenameSection(), IniWriteSection etc. Only you know the constraints of your particular INI so only you can write the code. It's not really something some generic code could handle. In my opinion. Phil Seakins Link to comment Share on other sites More sharing options...
jchd Posted August 14, 2020 Share Posted August 14, 2020 Ini convention doesn't cover sorting sections nor section content. So sorting an ini doesn't really make sense, in the general case. If order of sections or order of entries within a given section is important to you, then you might consider another file format, or perform the management of your choice by yourself. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Malkey Posted August 14, 2020 Share Posted August 14, 2020 This script sequentially renames all remaining sections of an .ini file after deleting one of the sections. expandcollapse popupLocal $sFileName = "TestIni.ini" ; --------- Create a test .ini file ---------- If FileExists($sFileName) Then FileDelete($sFileName) FileWrite($sFileName, StringRegExpReplace(FileRead(@ScriptFullPath), "(?s)(^.*#cs\R|\R#ce.*$)", "")) ; -------- End of Create .ini file ----------- _DeleteSect($sFileName, 2) Run('Notepad.exe "' & $sFileName & '"') Func _DeleteSect($sFilePath, $Sect) IniDelete($sFilePath, $Sect) $aArray = IniReadSectionNames($sFilePath) For $i = 1 To $aArray[0] IniRenameSection($sFilePath, $aArray[$i], $i) Next ; Format and save .ini file Local $sFileContents = FileRead($sFilePath) FileDelete($sFilePath) FileWrite($sFilePath, StringStripWS(StringRegExpReplace($sFileContents, "\s*(\[\d+\])", @CRLF & @CRLF & "$1"), 3) & @CRLF) ; (1) = strip leading white spaces, and, (2) = strip trailing white spaces EndFunc ;==>_DeleteSect #cs [1] s = 1 m = 4 [2] h = 2 j = 7 k = 9 l = 22 r = 99 [3] a = 0 l = 111 y = 88 w = 90 [4] d =0 #ce AlienStar 1 Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 21 hours ago, Malkey said: This script sequentially renames all remaining sections of an .ini file after deleting one of the sections. expandcollapse popupLocal $sFileName = "TestIni.ini" ; --------- Create a test .ini file ---------- If FileExists($sFileName) Then FileDelete($sFileName) FileWrite($sFileName, StringRegExpReplace(FileRead(@ScriptFullPath), "(?s)(^.*#cs\R|\R#ce.*$)", "")) ; -------- End of Create .ini file ----------- _DeleteSect($sFileName, 2) Run('Notepad.exe "' & $sFileName & '"') Func _DeleteSect($sFilePath, $Sect) IniDelete($sFilePath, $Sect) $aArray = IniReadSectionNames($sFilePath) For $i = 1 To $aArray[0] IniRenameSection($sFilePath, $aArray[$i], $i) Next ; Format and save .ini file Local $sFileContents = FileRead($sFilePath) FileDelete($sFilePath) FileWrite($sFilePath, StringStripWS(StringRegExpReplace($sFileContents, "\s*(\[\d+\])", @CRLF & @CRLF & "$1"), 3) & @CRLF) ; (1) = strip leading white spaces, and, (2) = strip trailing white spaces EndFunc ;==>_DeleteSect #cs [1] s = 1 m = 4 [2] h = 2 j = 7 k = 9 l = 22 r = 99 [3] a = 0 l = 111 y = 88 w = 90 [4] d =0 #ce after run the script it paste the script code into TestIni.ini !!! Link to comment Share on other sites More sharing options...
AlienStar Posted August 15, 2020 Author Share Posted August 15, 2020 (edited) I've found the solution after delete a section it checks if the section ID = size of ini sections => it deletes normally but if the section ID < size of ini sections we'll do a loop and rename sections after the deleted one to the older ID and etc.. $Site_db = Site_db.ini Local $del_arr = IniReadSectionNames($Site_db) ;-------------------------------------------- if $Sec_del_ID = UBound($del_arr)-1 then IniDelete($Site_db,$Sec_del_ID) ;------------------------------------------ Else ; else if $Sec_del_ID < UBound($del_arr)-1 ;------------------------------------------ iniDelete($Site_db,$Sec_del_ID) ;----------------------------------- Local $fread = FileRead($Site_db) Local $fopen = FileOpen($Site_db,2+8) ;----------------------------------- For $idl = $Sec_del_ID+1 to UBound($del_arr)-1 $fread = StringReplace($fread,"["&$idl&"]","["&$idl-1&"]") Next FileWrite($Site_db,$fread) FileClose($fopen) ;----------------------------------- Endif Edited August 15, 2020 by AlienStar Link to comment Share on other sites More sharing options...
AlienStar Posted August 18, 2020 Author Share Posted August 18, 2020 after I found the solution I've faced a new problem in this part when $Sec_del_ID < UBound($del_arr)-1 ;------------------------------------------ iniDelete($Site_db,$Sec_del_ID) ;----------------------------------- Local $fread = FileRead($Site_db) Local $fopen = FileOpen($Site_db,2+8) ;----------------------------------- For $idl = $Sec_del_ID+1 to UBound($del_arr)-1 $fread = StringReplace($fread,"["&$idl&"]","["&$idl-1&"]") Next FileWrite($Site_db,$fread) FileClose($fopen) ;----------------------------------- the file encoding changed from ansi to utf-8 and many section names are damaged how to keep file in ansi encoding please ?? Link to comment Share on other sites More sharing options...
rudi Posted August 21, 2020 Share Posted August 21, 2020 Hi. My 2 cent just use the native Ini*() functions: $ini="C:\temp\sample.ini" $aSectionNames=IniReadSectionNames($ini) $KickOut=2 $ShiftUp=False for $i = 1 to $aSectionNames[0] if $i = $KickOut Then ; this is the section to kick out $ShiftUp=True EndIf if $ShiftUp Then if $i = $aSectionNames[0] Then ; the last section has to be removed, its content has been moved up alreay at this point IniDelete($ini,$aSectionNames[$i]) Else $NextSectionName=$aSectionNames[$i] ; the section name to move the content of the following section to $a2ShiftContent=IniReadSection($ini,$aSectionNames[$i+1]) ; the content of the following section IniWriteSection($ini,$NextSectionName,$a2ShiftContent) ; overwrite with content of the following section EndIf EndIf Next ShellExecute("notepad.exe",$ini) Rudi. AlienStar 1 Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
rudi Posted August 21, 2020 Share Posted August 21, 2020 On 8/18/2020 at 9:20 PM, AlienStar said: after I found the solution I've faced a new problem in this part when $Sec_del_ID < UBound($del_arr)-1 ;------------------------------------------ iniDelete($Site_db,$Sec_del_ID) ;----------------------------------- Local $fread = FileRead($Site_db) Local $fopen = FileOpen($Site_db,2+8) ;----------------------------------- For $idl = $Sec_del_ID+1 to UBound($del_arr)-1 $fread = StringReplace($fread,"["&$idl&"]","["&$idl-1&"]") Next FileWrite($Site_db,$fread) FileClose($fopen) ;----------------------------------- the file encoding changed from ansi to utf-8 and many section names are damaged how to keep file in ansi encoding please ?? iirc 512 is opening a file to write ANSI: $ANSI=512 Local $fopen = FileOpen($Site_db,2+8+$ANSI) See the help file for fileopen, other values are given and explained there, too. Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
AlienStar Posted August 21, 2020 Author Share Posted August 21, 2020 1 hour ago, rudi said: iirc 512 is opening a file to write ANSI: $ANSI=512 Local $fopen = FileOpen($Site_db,2+8+$ANSI) See the help file for fileopen, other values are given and explained there, too. Rudi. I tried this but it failed also Link to comment Share on other sites More sharing options...
rudi Posted August 21, 2020 Share Posted August 21, 2020 what about trying the inireadsection() using code I've posted above? Inireadsection() and iniwritesection() might preserve the encoding "as-is" Regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
AlienStar Posted August 21, 2020 Author Share Posted August 21, 2020 8 hours ago, rudi said: Hi. My 2 cent just use the native Ini*() functions: $ini="C:\temp\sample.ini" $aSectionNames=IniReadSectionNames($ini) $KickOut=2 $ShiftUp=False for $i = 1 to $aSectionNames[0] if $i = $KickOut Then ; this is the section to kick out $ShiftUp=True EndIf if $ShiftUp Then if $i = $aSectionNames[0] Then ; the last section has to be removed, its content has been moved up alreay at this point IniDelete($ini,$aSectionNames[$i]) Else $NextSectionName=$aSectionNames[$i] ; the section name to move the content of the following section to $a2ShiftContent=IniReadSection($ini,$aSectionNames[$i+1]) ; the content of the following section IniWriteSection($ini,$NextSectionName,$a2ShiftContent) ; overwrite with content of the following section EndIf EndIf Next ShellExecute("notepad.exe",$ini) Rudi. it suppose to delete section [2] but it deletes the last section [4] !! Link to comment Share on other sites More sharing options...
rudi Posted September 7, 2020 Share Posted September 7, 2020 Maybe I didn't really get, what you need, for me it's doing what I want: Kickout the *CONTENT* of section 2 move up the content of all folloing sections up one section, preseving the original section names Remove the last section name (that one is empty after moving up all the other remaining section's content) Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
mikell Posted September 7, 2020 Share Posted September 7, 2020 On 8/15/2020 at 8:41 AM, AlienStar said: I've found the solution Maybe this ... (2 cents) $ini = "test.ini" $del = 2 $sn = IniReadSectionNames($ini) IniDelete($ini, $del) For $i = $del+1 to $sn[0] IniRenameSection($ini, $i, $i-1) Next $fe = FileGetEncoding($ini) $s = FileRead($ini) $hf = FileOpen($ini, 2 + $fe) FileWrite($hf, StringRegExpReplace($s, '\R+(?=\[)', @crlf & @crlf) ) FileClose($hf) AlienStar 1 Link to comment Share on other sites More sharing options...
AlienStar Posted September 8, 2020 Author Share Posted September 8, 2020 22 hours ago, mikell said: Maybe this ... (2 cents) $ini = "test.ini" $del = 2 $sn = IniReadSectionNames($ini) IniDelete($ini, $del) For $i = $del+1 to $sn[0] IniRenameSection($ini, $i, $i-1) Next $fe = FileGetEncoding($ini) $s = FileRead($ini) $hf = FileOpen($ini, 2 + $fe) FileWrite($hf, StringRegExpReplace($s, '\R+(?=\[)', @crlf & @crlf) ) FileClose($hf) this method is cool Link to comment Share on other sites More sharing options...
AlienStar Posted September 8, 2020 Author Share Posted September 8, 2020 On 9/7/2020 at 2:56 PM, rudi said: Maybe I didn't really get, what you need, for me it's doing what I want: Kickout the *CONTENT* of section 2 move up the content of all folloing sections up one section, preseving the original section names Remove the last section name (that one is empty after moving up all the other remaining section's content) you explained exactly what I need but I don't know why it deletes the last section instead of 2! Link to comment Share on other sites More sharing options...
mikell Posted September 8, 2020 Share Posted September 8, 2020 On 8/21/2020 at 2:04 PM, rudi said: My 2 cent just use the native Ini*() functions Works nice for me Link to comment Share on other sites More sharing options...
rudi Posted September 14, 2020 Share Posted September 14, 2020 @AlienStar I think I absolutely don't get your question at all! Step by step what the script does: Specify the number of the section, where the *SECTION CONTENT* shall be kicked out Here: the section #2, I named it [Section Two] If "arriving" at that section, *OVERWRITE* that section's content with the content of the *FOLLOWING* section Go on down the INI file to the section "last-but-one", overwriting all sections with the content of the following section If done so, the content of the last section was copied over to the "last-but-one-section" Finally there is a "last-section", holding the same content as the "last-but-one-section" Last step: Delete that last section as a clean up Maybe you want a different handling for the last section? I can see three options: Delete it (content was moved up one section, so I assumed the last section to be obsolete) Leave it "as-is", by that it will hold the same content as the "last-but-one-section" (duplicate information, IMHO a dirty result) "Blank" the section, so that just the section name without section content remains in the INI. What should that be good for? What is your goal? Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
AlienStar Posted September 16, 2020 Author Share Posted September 16, 2020 On 9/14/2020 at 12:38 PM, rudi said: @AlienStar I think I absolutely don't get your question at all! Step by step what the script does: Specify the number of the section, where the *SECTION CONTENT* shall be kicked out Here: the section #2, I named it [Section Two] If "arriving" at that section, *OVERWRITE* that section's content with the content of the *FOLLOWING* section Go on down the INI file to the section "last-but-one", overwriting all sections with the content of the following section If done so, the content of the last section was copied over to the "last-but-one-section" Finally there is a "last-section", holding the same content as the "last-but-one-section" Last step: Delete that last section as a clean up Maybe you want a different handling for the last section? I can see three options: Delete it (content was moved up one section, so I assumed the last section to be obsolete) Leave it "as-is", by that it will hold the same content as the "last-but-one-section" (duplicate information, IMHO a dirty result) "Blank" the section, so that just the section name without section content remains in the INI. What should that be good for? What is your goal? Rudi. sorry for delay but I had problems with my internet connection I've re test your code and it woks very well I don't know what the problem happened when I tested the first time thanks so much my friend and I deeply apologize for your effort Link to comment Share on other sites More sharing options...
rudi Posted September 17, 2020 Share Posted September 17, 2020 glad to help 😉 AlienStar 1 Earth is flat, pigs can fly, and Nuclear Power is SAFE! 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