Razormaul

Deleting value and key in ini file

6 posts in this topic

Hey all:)
 
I´ve tried... many things.
But im not good enough... yet!
 
Im trying to delete a selected set of Value&Key from an ini file.
It seems like an easy thing to do, but i cant make it work.
 
Here is what i´ve got.... Its a part of a much larger program, but its all you need.
(i know that, connect to server and ping does not Work either.

you can make your own ini or use this:

[servers]
MKNSRVTRAPEZE02=10.5.12.70
google.com=216.58.209.110

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;Global $hGuiAddServer = 9999 ; Dummystring
Local $aServers = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

; Create Main GUI and TabControl
$Form1 = GUICreate("IT Tools", 365, 553,@DesktopWidth - 380,@DesktopHeight - 1195)
$PageControl1 = GUICtrlCreateTab(8, 8, 350, 539)

; Servers Tab------------------------------------------------------------------------------------------------------------------------
$Servers = GUICtrlCreateTabItem("Servers")
$listview_srv = GUICtrlCreateListView("Server|IP", 20, 49, 222, 482, $LVS_SINGLESEL,$LVS_EX_GRIDLINES+$LVS_SORTDESCENDING)
_GuiCtrlListView_SetColumnWidth($listview_srv,0,130)
_GuiCtrlListView_SetColumnWidth($listview_srv,1,88)
;_GUICtrlListView_HideColumn($listview_srv, 2)
GetServers()

$btn_srv_AddServer = GUICtrlCreateButton("Add Server", 252, 51, 92, 25)
GUICtrlSetOnEvent(-1, "AddServer")
$btn_srv_RemoveServer = GUICtrlCreateButton("Remove Server", 252, 81, 94, 25)
GUICtrlSetOnEvent(-1, "RemoveServer")
$btn_srv_RefreshList = GUICtrlCreateButton("Refresh List", 252, 113, 94, 25)
GUICtrlSetOnEvent(-1, "RefreshServer")
$btn_srv_Connect = GUICtrlCreateButton("Connect", 252, 145, 94, 25)
GUICtrlSetOnEvent(-1, "ConnectToServer")
$btn_srv_Ping = GUICtrlCreateButton("Ping", 252, 178, 94, 25)
GUICtrlSetOnEvent(-1, "PingServer")

; Main GUI Control-------------------------------------------------------------------------------------------------------------------
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn_srv_AddServer
            addServer()
        Case $btn_srv_RefreshList
            RefreshServer()
        Case $btn_srv_RemoveServer
            RemoveServer()
        Case $btn_srv_Connect
            ConnectToServer()
        Case $btn_srv_Ping
            PingServer()

    EndSwitch
WEnd

; Add Server ------------------------------------------------------------------------------------------------------------------
Func addServer ()
    $hGuiAddServer = GUICreate ("Add Server", 100, 100, 100, 100)
    $NewServer = InputBox ("Add Server","Please type hostename","","","","",@DesktopWidth - 1100,@DesktopHeight - 800,"",$hGuiAddServer)

    TCPStartup()
    $ipAddress = TCPNameToIP ($NewServer)
    TCPShutdown()

    if Not $NewServer="" Then
        IniWrite (@ScriptDir & "\Servers.ini", "Servers", $NewServer, $ipAddress)
    EndIf

    While 2
        $sMsg = GUIGetMsg($NewServer)
            Switch $sMsg
        Case $GUI_EVENT_CLOSE
                GUICtrlDelete($hGuiAddServer)
            EndSwitch
        ExitLoop
    WEnd
        _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        GetServers()
EndFunc


; Remove Server --------------------------------------------------------------------------------------------------------------------
Func RemoveServer()
    Local $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
    Local $iniRead = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

    ;MsgBox (0, "",""&$sItem_srv&"")

    if not @error Then
        For $i = 1 To $iniRead[0][0]
            MsgBox (0,"List Hosts","Hostname: " & $iniRead[$i][0] & @CRLF & "IP: " & $iniRead[$i][1])
        Next
    EndIf


    ;IniDelete ("C:\it\Servers.ini","Servers",$sItem_srv)

    _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        GetServers()


EndFunc


; Connect to Server ---------------------------------------------------------------------------------------------------------------
Func ConnectToServer()
    $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))

    if $sItem_srv = "" Then
        MsgBox(0, "Connect to Server or Host","Please choose a host")
    Else

      $Host = StringSplit($sItem_srv,"|",1)
        MsgBox(0,"",$Host)

        ShellExecute("mstsc.exe","/admin /v "&$sItem_srv)
    EndIf

EndFunc


; Refresh Serverlist -------------------------------------------------------------------------------------------------------------
Func RefreshServer()
    _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
    GetServers()
EndFunc



; Get Servers from ini file --------------------------------------------------------------------------------------------------
Func GetServers()
    Local $aServers = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

    If Not @error Then
        For $i = 1 To $aServers[0][0]
            GUICtrlCreateListViewItem($aServers[$i][0] & '|' & $aServers[$i][1], $listview_srv)
            ;GUICtrlSetOnEvent(-1, '_ListViewHandler')
        Next
    EndIf
EndFunc


; Ping Server --------------------------------------------------------------------------------------------------

Func PingServer()

    $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        $sItem_srv = StringTrimRight($sItem_srv, 3)
        Local $iPing = Ping ($sItem_srv)

        ShellExecute("C:\Windows\System32\cmd.exe",$iPing)
EndFunc

Im loosing my hair!!

IT-Tools.V5_test.au3


Oh, my God. They found me. I don't know how, but they found me. Run for it, Marty!

Share this post


Link to post
Share on other sites



Im loosing my hair!!

Tension, Lite Sleep, Coding for hours are all causes of hair loss (Source: My Dad's Part Bald head)

I was look for this too, thanks for asking :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Your IniDelete function call is commented out in this reproducer script. 


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;Global $hGuiAddServer = 9999 ; Dummystring
Local $aServers = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

; Create Main GUI and TabControl
$Form1 = GUICreate("IT Tools", 365, 553, @DesktopWidth - 380,@DesktopHeight - 1195)
$PageControl1 = GUICtrlCreateTab(8, 8, 350, 539)

; Servers Tab------------------------------------------------------------------------------------------------------------------------
$Servers = GUICtrlCreateTabItem("Servers")
$listview_srv = GUICtrlCreateListView("Server|IP", 20, 49, 222, 482, $LVS_SINGLESEL,$LVS_EX_GRIDLINES+$LVS_SORTDESCENDING)
_GuiCtrlListView_SetColumnWidth($listview_srv,0,130)
_GuiCtrlListView_SetColumnWidth($listview_srv,1,88)
;_GUICtrlListView_HideColumn($listview_srv, 2)
GetServers()

$btn_srv_AddServer = GUICtrlCreateButton("Add Server", 252, 51, 92, 25)
GUICtrlSetOnEvent(-1, "AddServer")
$btn_srv_RemoveServer = GUICtrlCreateButton("Remove Server", 252, 81, 94, 25)
GUICtrlSetOnEvent(-1, "RemoveServer")
$btn_srv_RefreshList = GUICtrlCreateButton("Refresh List", 252, 113, 94, 25)
GUICtrlSetOnEvent(-1, "RefreshServer")
$btn_srv_Connect = GUICtrlCreateButton("Connect", 252, 145, 94, 25)
GUICtrlSetOnEvent(-1, "ConnectToServer")
$btn_srv_Ping = GUICtrlCreateButton("Ping", 252, 178, 94, 25)
GUICtrlSetOnEvent(-1, "PingServer")

; Main GUI Control-------------------------------------------------------------------------------------------------------------------
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn_srv_AddServer
            addServer()
        Case $btn_srv_RefreshList
            RefreshServer()
        Case $btn_srv_RemoveServer
            RemoveServer()
        Case $btn_srv_Connect
            ConnectToServer()
        Case $btn_srv_Ping
            PingServer()

    EndSwitch
WEnd

; Add Server ------------------------------------------------------------------------------------------------------------------
Func addServer ()
    $hGuiAddServer = GUICreate ("Add Server", 100, 100, 100, 100)
    $NewServer = InputBox ("Add Server","Please type hostename","","","","",@DesktopWidth - 1100,@DesktopHeight - 800,"",$hGuiAddServer)

    TCPStartup()
    $ipAddress = TCPNameToIP ($NewServer)
    TCPShutdown()

    if Not $NewServer="" Then
        IniWrite (@ScriptDir & "\Servers.ini", "Servers", $NewServer, $ipAddress)
    EndIf

    While 2
        $sMsg = GUIGetMsg($NewServer)
            Switch $sMsg
        Case $GUI_EVENT_CLOSE
                GUICtrlDelete($hGuiAddServer)
            EndSwitch
        ExitLoop
    WEnd
        _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        GetServers()
EndFunc


; Remove Server --------------------------------------------------------------------------------------------------------------------
Func RemoveServer()
    Local $hot_Item = _GUICtrlListView_GetSelectionMark($listview_srv)
    Local $sItem_srv = _GUICtrlListView_GetItemText($listview_srv, $hot_Item)
    Local $iniRead = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

    ;MsgBox (0, "",""&$sItem_srv&"")

    if not @error Then
        For $i = 1 To $iniRead[0][0]
            MsgBox (0,"List Hosts","Hostname: " & $iniRead[$i][0] & @CRLF & "IP: " & $iniRead[$i][1])
        Next
    EndIf

    IniDelete (@ScriptDir & "\Servers.ini","Servers",$sItem_srv)

    _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        GetServers()


EndFunc


; Connect to Server ---------------------------------------------------------------------------------------------------------------
Func ConnectToServer()
    $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))

    if $sItem_srv = "" Then
        MsgBox(0, "Connect to Server or Host","Please choose a host")
    Else

      $Host = StringSplit($sItem_srv,"|",1)
        MsgBox(0,"",$Host)

        ShellExecute("mstsc.exe","/admin /v "&$sItem_srv)
    EndIf

EndFunc


; Refresh Serverlist -------------------------------------------------------------------------------------------------------------
Func RefreshServer()
    _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
    GetServers()
EndFunc



; Get Servers from ini file --------------------------------------------------------------------------------------------------
Func GetServers()
    Local $aServers = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

    If Not @error Then
        For $i = 1 To $aServers[0][0]
            GUICtrlCreateListViewItem($aServers[$i][0] & '|' & $aServers[$i][1], $listview_srv)
            ;GUICtrlSetOnEvent(-1, '_ListViewHandler')
        Next
    EndIf
EndFunc


; Ping Server --------------------------------------------------------------------------------------------------

Func PingServer()

    $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        $sItem_srv = StringTrimRight($sItem_srv, 3)
        Local $iPing = Ping ($sItem_srv)

        ShellExecute("C:\Windows\System32\cmd.exe",$iPing)
EndFunc

You must delete them by the key name, which you were trying to delete by the Key & Value name.

All good? :)

EDIT: Fixed grammar.

Edited by MikahS
1 person likes this

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;Global $hGuiAddServer = 9999 ; Dummystring
Local $aServers = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

; Create Main GUI and TabControl
$Form1 = GUICreate("IT Tools", 365, 553, @DesktopWidth - 380,@DesktopHeight - 1195)
$PageControl1 = GUICtrlCreateTab(8, 8, 350, 539)

; Servers Tab------------------------------------------------------------------------------------------------------------------------
$Servers = GUICtrlCreateTabItem("Servers")
$listview_srv = GUICtrlCreateListView("Server|IP", 20, 49, 222, 482, $LVS_SINGLESEL,$LVS_EX_GRIDLINES+$LVS_SORTDESCENDING)
_GuiCtrlListView_SetColumnWidth($listview_srv,0,130)
_GuiCtrlListView_SetColumnWidth($listview_srv,1,88)
;_GUICtrlListView_HideColumn($listview_srv, 2)
GetServers()

$btn_srv_AddServer = GUICtrlCreateButton("Add Server", 252, 51, 92, 25)
GUICtrlSetOnEvent(-1, "AddServer")
$btn_srv_RemoveServer = GUICtrlCreateButton("Remove Server", 252, 81, 94, 25)
GUICtrlSetOnEvent(-1, "RemoveServer")
$btn_srv_RefreshList = GUICtrlCreateButton("Refresh List", 252, 113, 94, 25)
GUICtrlSetOnEvent(-1, "RefreshServer")
$btn_srv_Connect = GUICtrlCreateButton("Connect", 252, 145, 94, 25)
GUICtrlSetOnEvent(-1, "ConnectToServer")
$btn_srv_Ping = GUICtrlCreateButton("Ping", 252, 178, 94, 25)
GUICtrlSetOnEvent(-1, "PingServer")

; Main GUI Control-------------------------------------------------------------------------------------------------------------------
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn_srv_AddServer
            addServer()
        Case $btn_srv_RefreshList
            RefreshServer()
        Case $btn_srv_RemoveServer
            RemoveServer()
        Case $btn_srv_Connect
            ConnectToServer()
        Case $btn_srv_Ping
            PingServer()

    EndSwitch
WEnd

; Add Server ------------------------------------------------------------------------------------------------------------------
Func addServer ()
    $hGuiAddServer = GUICreate ("Add Server", 100, 100, 100, 100)
    $NewServer = InputBox ("Add Server","Please type hostename","","","","",@DesktopWidth - 1100,@DesktopHeight - 800,"",$hGuiAddServer)

    TCPStartup()
    $ipAddress = TCPNameToIP ($NewServer)
    TCPShutdown()

    if Not $NewServer="" Then
        IniWrite (@ScriptDir & "\Servers.ini", "Servers", $NewServer, $ipAddress)
    EndIf

    While 2
        $sMsg = GUIGetMsg($NewServer)
            Switch $sMsg
        Case $GUI_EVENT_CLOSE
                GUICtrlDelete($hGuiAddServer)
            EndSwitch
        ExitLoop
    WEnd
        _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        GetServers()
EndFunc


; Remove Server --------------------------------------------------------------------------------------------------------------------
Func RemoveServer()
    Local $hot_Item = _GUICtrlListView_GetSelectionMark($listview_srv)
    Local $sItem_srv = _GUICtrlListView_GetItemText($listview_srv, $hot_Item)
    Local $iniRead = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

    ;MsgBox (0, "",""&$sItem_srv&"")

    if not @error Then
        For $i = 1 To $iniRead[0][0]
            MsgBox (0,"List Hosts","Hostname: " & $iniRead[$i][0] & @CRLF & "IP: " & $iniRead[$i][1])
        Next
    EndIf

    IniDelete (@ScriptDir & "\Servers.ini","Servers",$sItem_srv)

    _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        GetServers()


EndFunc


; Connect to Server ---------------------------------------------------------------------------------------------------------------
Func ConnectToServer()
    $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))

    if $sItem_srv = "" Then
        MsgBox(0, "Connect to Server or Host","Please choose a host")
    Else

      $Host = StringSplit($sItem_srv,"|",1)
        MsgBox(0,"",$Host)

        ShellExecute("mstsc.exe","/admin /v "&$sItem_srv)
    EndIf

EndFunc


; Refresh Serverlist -------------------------------------------------------------------------------------------------------------
Func RefreshServer()
    _GUICtrlListView_DeleteAllItems($listview_srv)
        $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
    GetServers()
EndFunc



; Get Servers from ini file --------------------------------------------------------------------------------------------------
Func GetServers()
    Local $aServers = IniReadSection (@ScriptDir & "\Servers.ini", "Servers")

    If Not @error Then
        For $i = 1 To $aServers[0][0]
            GUICtrlCreateListViewItem($aServers[$i][0] & '|' & $aServers[$i][1], $listview_srv)
            ;GUICtrlSetOnEvent(-1, '_ListViewHandler')
        Next
    EndIf
EndFunc


; Ping Server --------------------------------------------------------------------------------------------------

Func PingServer()

    $sItem_srv = GUICtrlRead(GUICtrlRead($listview_srv))
        $sItem_srv = StringTrimRight($sItem_srv, 3)
        Local $iPing = Ping ($sItem_srv)

        ShellExecute("C:\Windows\System32\cmd.exe",$iPing)
EndFunc

You must delete them by the key name, which you were trying to delete by the Key & Value name.

All good? :)

EDIT: Fixed grammar.

All Good :lmao:

Thanks a million.


Oh, my God. They found me. I don't know how, but they found me. Run for it, Marty!

Share this post


Link to post
Share on other sites

My pleasure. ;)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
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