Jump to content

move section within INI file?


tcox8
 Share

Recommended Posts

Hello all,

I have a problem with editing my ini file. I need the section name and keys/values that go along with that section to be added to another section in the ini file, not added to the bottom of the ini file.

Example INI:

;========================
; Beta Image List
;========================
[BetaImageNames]
0=Section1
1=Section2
 
;========================
; Beta Image Details
;========================
[Section1]
Name=name1
Diskpart=Diskpart7B
OS=Win7
Name2=Windows7_master_1
 
[Section2]
Name=name2
Diskpart=Diskpart7B
OS=Win7
Name2=Windows7_master_1
 
 
;========================
; Archived Image List
;========================
[ArchImageNames]
0=Section1
1=Section2
 
;========================
; Archived Image Details
;========================
[Section1]
Name=name1
Diskpart=Diskpart7B
OS=Win7
Name2=Windows7_master_1
 
[Section2]
Name=name2
Diskpart=Diskpart7B
OS=Win7
Name2=Windows7_master_1

Now let's say I wanted to added a "Section3" to "Beta Image Details". What I have now (listed below) adds the Section to the very end of the .ini file.

Func _WriteINI($varType,$varImageName,$varOS,$varUpdate,$varDiskPart)
$var = InireadSection($ini,$varType) ;read the ini section to get list of images
  If @error Then
   MsgBox(4096, "Error!", "Unable to locate INI file or section within INI file!")
  Else
   If $varUpdate = "no" Then ;ignore this if statement!
    For $i = 1 To $var[0][0]
     If $var[$i][1] = $varImageName then MsgBox(0,"Uh-Oh!","Uh-Oh! This name already exists!") ;does name alread exist?
    Next
   Else
    Sleep(10)
   EndIf
  EndIf
IniWriteSection($ini,$varImageName,"",$var[0][0]) ;create the section
$write = IniWrite($ini, $varType, $var[0][0], $varImageName) ;write the image type
  If $write <> 1 Then MsgBox(0,"Error!","Error writing to INI file!")
$write1 = IniWrite($ini, $varImageName, "Name", $varImageName) ;write the image name
  If $write1 <> 1 Then MsgBox(0,"Error!","Error writing to INI file!")
$write2 = IniWrite($ini, $varImageName, "Diskpart",$varDiskPart) ;write the diskpart script used
  If $write2 <> 1 Then MsgBox(0,"Error!","Error writing to INI file!")
$write3 = IniWrite($ini, $varImageName, "OS", $varOS) ;write the OS used for image
  If $write3 <> 1 Then MsgBox(0,"Error!","Error writing to INI file!")
If $varOS = "Win7" Then ;determines if we need the second image for Win7 OS's
  If $varType = "StuImageNames" Then
   $imageTWO = "Windows7_student_master_1" ;name of second image
  ElseIf $varType = "FacImageNames" Then
   $imageTWO = "Windows7_Universal_1" ;name of second image
  EndIf
  $write4 = IniWrite($ini, $varImageName, "Name2", $imageTWO) ;write second image if used
  If $write4 <> 1 Then MsgBox(0,"Error!","Error writing to INI file!")
EndIf
$write5 = IniWrite($ini,"About", "UpdatedDate", @MON & "/" & @MDAY & "/" & @YEAR) ;let's change the update date.
  If $write5 <> 1 Then MsgBox(0,"Error!","Error writing to INI file!")
_popLists() ;update the lists to reflect our new image!
EndFunc ;==> _WriteINI()

Any ideas? Thanks!

Edited by tcox8

"There are only 10 types of people in the world: Those who understand binary, and those who don't"

Link to comment
Share on other sites

You could read each line and then write it to a new file and then insert the section after finding the right location where you want to drop the new section

but you are going to have issues with having the same section name - as which one is it going to pick?

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Wouldn't function InIWriteSection do what you need?

No, this will create the section but it adds it to the end of the file. I want to add the section to the middle of the file

"There are only 10 types of people in the world: Those who understand binary, and those who don't"

Link to comment
Share on other sites

I would suggest two ini files. One with the archived sections and one with all other entries.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I would suggest two ini files. One with the archived sections and one with all other entries.

Yes I considered this, but I was really hoping to use one file. Seems that it may be easiest just to have 2 files.

"There are only 10 types of people in the world: Those who understand binary, and those who don't"

Link to comment
Share on other sites

Yes I considered this, but I was really hoping to use one file. Seems that it may be easiest just to have 2 files.

You can still use one file, but you have to have unique names for your sections - in either case - unless you rewrite the file with your enteries where you want them - it is going to append to the end of the file.

EDIT - you could prepopulate the section - where you want them to be, then when you write the section (as per @water) then it will populate where you want it.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Considering that the ini files don't care where the sections/keys/values are located inside the file, does it really matter (other than being overly concerned with neatness) where it places the information? Your second section 3 isn't going to be read anyways, so it doesn't really matter where you put it, does it?

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

Considering that the ini files don't care where the sections/keys/values are located inside the file, does it really matter (other than being overly concerned with neatness) where it places the information? Your second section 3 isn't going to be read anyways, so it doesn't really matter where you put it, does it?

Yes it matters, Since I will be reading the ini file manually I would like to keep the sections under their names accordingly. i.e. keeping the beta images listed under the beta section.

"There are only 10 types of people in the world: Those who understand binary, and those who don't"

Link to comment
Share on other sites

EDIT - you could prepopulate the section - where you want them to be, then when you write the section (as per @water) then it will populate where you want it.

This won't work for me because I do not know how many images and/or when I will be needing more added in the ini file until the time the image is created (and then added to the ini file).

"There are only 10 types of people in the world: Those who understand binary, and those who don't"

Link to comment
Share on other sites

There is good info about IniFunctions in the help file.

$aSection = IniReadSection("My_IniFile.ini", "Section 1")
IniWriteSection("My_IniFile.ini", "Section 2", $aSection)
IniDelete("My_IniFile.ini", "Section 1")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Yes it matters, Since I will be reading the ini file manually I would like to keep the sections under their names accordingly. i.e. keeping the beta images listed under the beta section.

Does that mean you're not using IniRead or IniReadSection? If so, then don't use IniWrite, or IniWriteSection use FileWriteLine, or FileWrite and rewrite the INI file everytime because that's probably the only way you're going to be able to control where in the file the data is added.

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

Let the main script handle the ini file using AutoIt builtin functions (resulting in an unordered ini file).

As you just want to read the sorted ini file I would write a second script that creates just what you need.

Use to IniReadSectionNames, sort the returned array, loop through the array and read every section using InIReadSection and write it to a temporary ini file using InIWriteSection. You now have an ordered file you can display.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

An ini is not designed as a database. Why don't you create your own file format or use SQLite?

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

There is good info about IniFunctions in the help file.

$aSection = IniReadSection("My_IniFile.ini", "Section 1")
IniWriteSection("My_IniFile.ini", "Section 2", $aSection)
IniDelete("My_IniFile.ini", "Section 1")

This actually is not what i'm looking for. I already read the help file on this. But thanks!

I think I will edit a few lines and redesign the way I'm going to look at my ini file. i.e. create unique sections and just add keys/values under the sections to tell me what type of image I am looking at. that way I can just let autoit sort the sections how it pleases.

Thanks everyone for your help! I'm always amazed at the massive amount of help that is offered here in the autoit community! :-)

"There are only 10 types of people in the world: Those who understand binary, and those who don't"

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