Jump to content

View/edit Ini Files


strate
 Share

Recommended Posts

This is a script that I've created for one of my programs so that a user unfimiliar with INI files could change the values. This is my first contribution to the site so be gentle. I made it for my needs but see no reason in adding to it. Any recommendations would be great! It will work for any* INI file.

*Known problem: It crashes when creating the treeview for a LARGE ini that consists of 2K sections. Help solving that would be great also.

Beta is needed.

#include <GUIConstants.au3>
#Include <GuiTreeView.au3>
#include <File.au3>

$INIFile = 'C:\GUI Label Entry.ini'

$EditMainGUI = GUICreate("View/Edit INI File", 350, 193)

$treeview = GUICtrlCreateTreeView(6, 6, 230, 180, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

_ReadinINI()

$b_INIView = GUICtrlCreateButton("&View/Edit", 245, 6, 100, 20)
$b_INIColExp = GUICtrlCreateButton("&Collapse./Expand.", 245, 30, 100, 20)
$b_INIPrint = GUICtrlCreateButton("&Print INI File", 245, 54, 100, 20)
$b_INIExit = GUICtrlCreateButton("&Exit", 245, 78, 100, 20)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $b_INIExit Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $b_INIView
            $item = GUICtrlRead($treeview)  ; Get the controlID of the current selected treeview item
            If $item = 0 Then
                _HideGUIMSGBoxShowGUI(64, "INI Edit", "No item currently selected.")
            Else
                $advmsg = GUICtrlRead($item, 1); Get advanced infos about the treeview item
                If ($advmsg[0] == 0) Then
                    _HideGUIMSGBoxShowGUI(16, "INI Edit: Error", "Error while retrieving info about selected item.")
                Else
                    Global $Value = IniRead($INIFile, _GUICtrlTreeViewGetText($treeview, _GUICtrlTreeViewGetParentHandle($treeview)), $advmsg[0], 'ERROR')
                    GUISetState(@SW_HIDE)
                    $Edit = MsgBox(48 + 4096 + 4, "INI Edit", "Current key selected:   " & $advmsg[0] & @CR _
                             & '         With a value of:   ' & $Value & @CR _
                             & @CR _
                             & 'Do you want to edit this value?')
                    If $Edit = 6 Then; User wants to edit the value
                        _EditValue()
                    Else
                        GUISetState(@SW_SHOW)
                    EndIf
                EndIf
            EndIf
        Case $msg = $b_INIColExp
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $hItem = GUICtrlGetHandle($item)
                GuiCtrlSendMsg($treeview, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
            EndIf
        Case $msg = $b_INIPrint
            _FilePrint ($INIFile)
    EndSelect
WEnd

GUIDelete()
Exit
Func _HideGUIMSGBoxShowGUI($MsgBoxOptions, $MsgBoxTitle, $MsgBoxText)
    GUISetState(@SW_HIDE)
    Global $MsgAsr = MsgBox($MsgBoxOptions, $MsgBoxTitle, $MsgBoxText)
    GUISetState(@SW_SHOW)
    Return $MsgAsr
EndFunc ;==>_HideGUIMSGBoxShowGUI
Func _EditValue()
    $EditGUI = GUICreate("INI Edit", 392, 89, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
    GUICtrlCreateLabel("Value:", 10, 10, 30, 20)
    $Input_EditValue = GUICtrlCreateInput($Value, 10, 30, 370, 20)
    $b_EditValue_Save = GUICtrlCreateButton("Save", 10, 60, 50, 20)
    $b_EditValue_Restore = GUICtrlCreateButton("Restore Original Value", 70, 60, 120, 20)
    $b_EditValue_Exit = GUICtrlCreateButton("Exit", 200, 60, 50, 20)
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $b_EditValue_Exit
                ExitLoop
            Case $msg = $b_EditValue_Restore
                GUICtrlSetData($Input_EditValue, $Value)
                GUICtrlSetState($Input_EditValue, $GUI_FOCUS)
            Case $msg = $b_EditValue_Save
                $Edit = MsgBox(48 + 4096 + 4, "INI Edit", "Are you sure you want to replace:" & @CR _
                         & '          ' & $Value & @CR _
                         & 'With:' & @CR _
                         & '          ' & GUICtrlRead($Input_EditValue))
                If $Edit = 6 Then; User wants to edit the value
                    $Err = IniWrite($INIFile, _GUICtrlTreeViewGetText($treeview, _GUICtrlTreeViewGetParentHandle($treeview)), $advmsg[0], GUICtrlRead($Input_EditValue))
                    If $Err = 0 Then MsgBox(4096, 'Error Occured', 'New value was NOT added to the INI File.')
                    GUISwitch($EditMainGUI)
                    _GUICtrlTreeViewExpand($treeview, 0)
                    GUISwitch($EditGUI)
                    ExitLoop
                EndIf
        EndSelect
    WEnd
    GUIDelete()
    GUISetState(@SW_SHOW)
EndFunc ;==>_EditValue
Func _ReadinINI()
    $var = IniReadSectionNames($INIFile)
    If @error Then
        MsgBox(4096, "", "Error occured, probably no INI file.")
    Else
        Dim $generalitem[1]
        For $i = 1 To $var[0]
            ReDim $generalitem[UBound($generalitem) + 1]
            $generalitem [UBound($generalitem) - 1] = GUICtrlCreateTreeViewItem($var[$i], $treeview)
            GUICtrlSetState(-1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
            GUICtrlSetColor(-1, 0x0000C0)
            $var1 = IniReadSection($INIFile, $var[$i])
            If @error Then
                MsgBox(4096, "", "Error occured, probably no INI file.")
                Exit
            Else
                For $ii = 1 To $var1[0][0]
                    $DetailItem = GUICtrlCreateTreeViewItem($var1[$ii][0], $generalitem[$i])
                Next
            EndIf
        Next
    EndIf
EndFunc ;==>_ReadinINI
Edited by strate
INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...
Link to comment
Share on other sites

So someone did a treeview for one!

Well... look at by (n00bish) one in my sig: Under UDFs : _DialogEditIni()

#)

I like that, never thought about the adding, removing sections, and a save as. I think yours might intimidate some of my coworkers, lol.
INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...
Link to comment
Share on other sites

i couldn't get either one of nfwu's .. t.au3 or the posted code to wrk... just kept gwtting errors.. anyways i played with yours alittle

it read my 493 line ini... right off the bat... but they are just numbers, so i added the actual value to the script

#include <GUIConstants.au3>
#Include <GuiTreeView.au3>
#include <File.au3>

$INIFile = @ScriptDir & "\lang.lng"

$EditMainGUI = GUICreate("View/Edit INI File", 350, 193)

$treeview = GUICtrlCreateTreeView(6, 6, 230, 180, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

_ReadinINI()

$b_INIView = GUICtrlCreateButton("&View/Edit", 245, 6, 100, 20)
$b_INIColExp = GUICtrlCreateButton("&Collapse./Expand.", 245, 30, 100, 20)
$b_INIPrint = GUICtrlCreateButton("&Print INI File", 245, 54, 100, 20)
$b_INIExit = GUICtrlCreateButton("&Exit", 245, 78, 100, 20)

GUISetState()
; Or StringLeft($item, 3) = "*= "
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $b_INIExit Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $b_INIView
            $item = GUICtrlRead($treeview)   ; Get the controlID of the current selected treeview item
            If $item = 0 Then
                _HideGUIMSGBoxShowGUI(64, "INI Edit", "No item currently selected.")
            Else
                $advmsg = GUICtrlRead($item, 1); Get advanced infos about the treeview item
                If ($advmsg[0] == 0) Then 
                    _HideGUIMSGBoxShowGUI(16, "INI Edit: Error", "Error while retrieving info about selected item.")
                ElseIf StringLeft($advmsg[0], 7) == "Value= " Then
                    MsgBox(64, "INI Value", "Please Select the Corresponding key   ", 2)  
                Else
                    Global $Value = IniRead($INIFile, _GUICtrlTreeViewGetText($treeview, _GUICtrlTreeViewGetParentHandle($treeview)), $advmsg[0], 'ERROR')
                    GUISetState(@SW_HIDE)
                    $Edit = MsgBox(48 + 4096 + 4, "INI Edit", "Current key selected:   " & $advmsg[0] & @CR _
                             & '         With a value of:   ' & $Value & @CR _
                             & @CR _
                             & 'Do you want to edit this value?')
                    If $Edit = 6 Then; User wants to edit the value
                        _EditValue()
                    Else
                        GUISetState(@SW_SHOW)
                    EndIf
                EndIf
            EndIf
        Case $msg = $b_INIColExp
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $hItem = GUICtrlGetHandle($item)
                GuiCtrlSendMsg($treeview, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
            EndIf
        Case $msg = $b_INIPrint
            _FilePrint ($INIFile)
    EndSelect
WEnd

GUIDelete()
Exit
Func _HideGUIMSGBoxShowGUI($MsgBoxOptions, $MsgBoxTitle, $MsgBoxText)
    GUISetState(@SW_HIDE)
    Global $MsgAsr = MsgBox($MsgBoxOptions, $MsgBoxTitle, $MsgBoxText)
    GUISetState(@SW_SHOW)
    Return $MsgAsr
EndFunc;==>_HideGUIMSGBoxShowGUI
Func _EditValue()
    $EditGUI = GUICreate("INI Edit", 392, 89, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
    GUICtrlCreateLabel("Value:", 10, 10, 30, 20)
    $Input_EditValue = GUICtrlCreateInput($Value, 10, 30, 370, 20)
    $b_EditValue_Save = GUICtrlCreateButton("Save", 10, 60, 50, 20)
    $b_EditValue_Restore = GUICtrlCreateButton("Restore Original Value", 70, 60, 120, 20)
    $b_EditValue_Exit = GUICtrlCreateButton("Exit", 200, 60, 50, 20)
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $b_EditValue_Exit
                ExitLoop
            Case $msg = $b_EditValue_Restore
                GUICtrlSetData($Input_EditValue, $Value)
                GUICtrlSetState($Input_EditValue, $GUI_FOCUS)
            Case $msg = $b_EditValue_Save
                $Edit = MsgBox(48 + 4096 + 4, "INI Edit", "Are you sure you want to replace:" & @CR _
                         & '          ' & $Value & @CR _
                         & 'With:' & @CR _
                         & '          ' & GUICtrlRead($Input_EditValue))
                If $Edit = 6 Then; User wants to edit the value
                    $Err = IniWrite($INIFile, _GUICtrlTreeViewGetText($treeview, _GUICtrlTreeViewGetParentHandle($treeview)), $advmsg[0], GUICtrlRead($Input_EditValue))
                    If $Err = 0 Then MsgBox(4096, 'Error Occured', 'New value was NOT added to the INI File.')
                    GUISwitch($EditMainGUI)
                    _GUICtrlTreeViewExpand($treeview, 0)
                    GUISwitch($EditGUI)
                    ExitLoop
                EndIf
        EndSelect
    WEnd
    GUIDelete()
    GUISetState(@SW_SHOW)
EndFunc;==>_EditValue
Func _ReadinINI()
    $var = IniReadSectionNames($INIFile)
    If @error Then
        MsgBox(4096, "", "Error occured, probably no INI file.")
    Else
        Dim $generalitem[1]
        For $i = 1 To $var[0]
            ReDim $generalitem[UBound($generalitem) + 1]
            $generalitem [UBound($generalitem) - 1] = GUICtrlCreateTreeViewItem($var[$i], $treeview)
            GUICtrlSetState(-1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
            GUICtrlSetColor(-1, 0x0000C0)
            $var1 = IniReadSection($INIFile, $var[$i])
            If @error Then
                MsgBox(4096, "", "Error occured, probably no INI file.")
                Exit
            Else
                For $ii = 1 To $var1[0][0]
                    $DetailItem = GUICtrlCreateTreeViewItem($var1[$ii][0], $generalitem[$i])
                    GUICtrlSetState(-1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
                    GUICtrlSetColor(-1, 0x0000C0)
                    $look = GUICtrlCreateTreeViewItem("Value= " &$var1[$ii][1], $generalitem[$i])
                Next
            EndIf
        Next
    EndIf
EndFunc;==>_ReadinINI

8)

NEWHeader1.png

Link to comment
Share on other sites

i couldn't get either one of nfwu's .. t.au3 or the posted code to wrk... just kept gwtting errors.. anyways i played with yours alittle

You require to input a valid ini file into the function... sorry but it does not create inis...

#)

Link to comment
Share on other sites

You require to input a valid ini file into the function... sorry but it does not create inis...

#)

i used this

_DialogEditIni(@ScriptDir & "\lang.lng")

for one

and..

_EditIniDialog(@ScriptDir & "\lang.lng")

for the other

i got all kinds of errors

example

from posted code 
ElseIf $msg == $SectionDelButton Or $msg == $SectionDelButton Or _
                        $msg == $SectionEdiButton Or $msg == $PairNewButton Or _
                        $msg == $PairDelButton Or $msg == $PairEdiButton Or $msg == $SaveSavButton
                    MsgBox(262160, $EIDtitle & " - Error", "You are in (Read-Only) Mode." & @CRLF & "You are not allowed to edit the data")

there is no "Then"

8)

NEWHeader1.png

Link to comment
Share on other sites

I see now... thanks for pointing out my syntax errors...

Edit: Come to think of that it is wierd... I used the exact same code to produce the screenshot...

#)

Edited by nfwu
Link to comment
Share on other sites

this is really nice. Good work!

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

i have now, that is much better

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

  • 11 months later...

Hi.

I dont know if anyone can help me. But when I tried to use this script. It did everything fine but edit the ini file. It came up with the error

"Subscript used with non-Array variable."

If ($advmsg[0] == 0) Then

If ($advmsg^ ERROR

It seems to stop on this line. Any ideas?

CheersGrant

Link to comment
Share on other sites

  • 2 weeks later...

Hi.

I dont know if anyone can help me. But when I tried to use this script. It did everything fine but edit the ini file. It came up with the error

"Subscript used with non-Array variable."

If ($advmsg[0] == 0) Then

If ($advmsg^ ERROR

It seems to stop on this line. Any ideas?

I think it's because of this:

$advmsg = GUICtrlRead($item, 1); Get advanced infos about the treeview item
                If ($advmsg[0] == 0) Then 
                     ; ...
                EndIf

I don't think GuiCtrlRead() ever returns an array, even for advanced mode. So the array reference is inappropriate. In this code, $item is the control ID of a TreeViewItem, and GuiCtrlRead($item, 1) returns the text of $item.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...