Jump to content

Need help Reading , Editing , & Removing Line from *.ini and *.txt files


HiSoftDev
 Share

Recommended Posts

hi friends,

i have a *.ini file which contains this

F:\FileTest.exe

E:\All Softwares\\GoogleEarthWinProSetup.exe

C:\AdobeRd9.0.exe

D:\AutoIt\\File1.exe

now i want to read it , removing some lines from it, and/or editing it.

something like this.

Posted Image

i want to get Line number, Program Name (Exe files in Paths), FilePath information and show it as shown in the ScreenShot.

Add button is for adding more lines at the end of *.ini files.

Remove button is for removing the selected line from the *.ini files.

can any of you give me some example, some code of peace , idea, link, example script, or something which can help me?

i have read the HelpFile but the functions related to reading, writing, editing ini files are not coming in mind.

any help would be really appreciated and lot of thanks in advance for your good job and support to newbies.

thanks once again...

Edit: there were some extra slashes\\\ in BB code, now changed to

Edited by HiSoftDev
Link to comment
Share on other sites

Here what I came up with, now sure what ya really wanted but I gave it a shot

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         Rental

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>

;----------------------------------------------
;Collect data from Ini sections
;----------------------------------------------
$path1 = IniRead("new.ini", "index", "one", 1)
$path2 = IniRead("new.ini", "index", "two", "2")
$path3 = IniRead("new.ini", "index", "three", "3")
$path4 = IniRead("new.ini", "index", "four", "4")



;--------------------------------------------------------------
;Taking the date collected and seperating the file from folder
;--------------------------------------------------------------

;Data 1
$len1 = StringLen($path1)
$len1_1 = 12
$len1_2 = 3
$cut1_1 = StringLeft($path1, $len1 - $len1_1)
$cut1_2 = StringRight($path1, $Len1 - $len1_2)

;Data 2
$len2 = StringLen($path2)
$len2_1 =18
$len2_2 = 27
$cut2_1 = StringRight($path2, $len2 - $Len2_1)
$cut2_2 = StringLeft($path2, $len2 - $len2_2)

;Data 3
$len3 = StringLen($path3)
$len3_1 =3
$len3_2 = 14
$cut3_1 = StringRight($path3, $len3 - $Len3_1)
$cut3_2 = StringLeft($path3, $len3 - $len3_2)

;Data 4
$len4 = StringLen($path4)
$len4_1 =11
$len4_2 = 9
$cut4_1 = StringRight($path4, $len4 - $Len4_1)
$cut4_2 = StringLeft($path4, $len4 - $len4_2)



;-------------
;GUI Display
;-------------

    GUICreate("Showing the Data")  ; will create a dialog box that when displayed is centered

$listView = GuiCtrlCreateListView(" # |Program Name| Program Path", 10, 10, 380, 180)
GuiCtrlCreateListViewItem("1|"&$cut1_2&"|"&$cut1_1, $listView)
GuiCtrlCreateListViewItem("2|"&$cut2_1&"|"&$cut2_2, $listView)
GuiCtrlCreateListViewItem("3|"&$cut3_1&"|"&$cut3_2, $listView)
GuiCtrlCreateListViewItem("4|"&$cut4_1&"|"&$cut4_2, $listView)

    GUISetState()

    
    While 1
        $msg = GUIGetMsg()
        
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd

Posted Image

As for the ini file this is what I used because i didnt know what your section or key was

[index]
one=F:\filetest.exe
two=E:\All Softwares\\GoogleEearthWinProSetup.exe
three=C:\AdobeRd9.0.exe
four=D:\AutoIt\\File1.exe

and last, I didn't put any buttons on there.

Edited by Rental
Link to comment
Share on other sites

now the problem is that my ini file contains no section or keys (only values exists which are path to the files as i posted)

my second problem is that i know how to add button to remove values from file and add new values button, but i don\'t know how these buttons will work (i mean i can create the buttons but the code behind them for modifying ini files is unknown for me, see buttons shown in image of my first post)

a lot of thanks for your help and , information and i know you worked on code for me which is really appreciated.

can you please help me out?

thanks once again...

Link to comment
Share on other sites

HiSoftDev

Quick example:

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

$hGUI = GUICreate("Test", 300, 200)

$hListView = GUICtrlCreateListView("#|Program name|Program path", 10, 10, 280, 150)

$OkButton = GUICtrlCreateButton("Ok", 50, 170, 75, 23)

$AddButton = GUICtrlCreateButton("Add", 130, 170, 75, 23)

$RemButton = GUICtrlCreateButton("Remove", 210, 170, 75, 23)

$AddGUI = GUICreate("Add new string", 300, 100, -1, -1, -1, -1, $hGUI)

$input = GUICtrlCreateInput("", 10, 20, 280, 20)

$AddStr_Button = GUICtrlCreateButton("Add", 10, 60, 75, 23)

$Close_Button = GUICtrlCreateButton("Close", 215, 60, 75, 23)

GUISetState(@SW_SHOW, $hGUI)

_FillList(@ScriptDir & "\test.ini")

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $GUI_EVENT_CLOSE, $Close_Button
            If $msg[1] = $hGUI Then
                Exit
            Else
                GUISetState(@SW_ENABLE, $hGUI)
                GUISetState(@SW_HIDE, $AddGUI)
            EndIf
        Case $OkButton
            $sData = ""
            For $i = 0 To _GUICtrlListView_GetItemCount($hListView) - 1
                $sData &= _GUICtrlListView_GetItemText($hListView, $i, 2) & _GUICtrlListView_GetItemText($hListView, $i, 1) & @CRLF
            Next
            
            $hFile = FileOpen(@ScriptDir & "\test.ini", 2)
            FileWrite($hFile, $sData)
            FileClose($hFile)
            Exit
        Case $AddButton
            GUICtrlSetData($input, "")
            
            GUISetState(@SW_DISABLE, $hGUI)
            GUISetState(@SW_SHOW, $AddGUI)
        Case $RemButton
            _GUICtrlListView_DeleteItemsSelected($hListView)
        Case $AddStr_Button
            GUICtrlCreateListViewItem(GUICtrlRead($input), $hListView)
            
            GUISetState(@SW_ENABLE, $hGUI)
            GUISetState(@SW_HIDE, $AddGUI)
    EndSwitch
WEnd

Func _FillList($sFile)
    Local $iRead = FileRead($sFile)
    Local $aString = StringSplit(StringStripCR($iRead), @LF)
    
    For $i = 1 To $aString[0]
        GUICtrlCreateListViewItem($i & "|" & StringRegExpReplace($aString[$i], "^.*\\", "") & "|" & _
                                  StringRegExpReplace($aString[$i], "(^.*\\).*", "\1"), $hListView)
    Next
EndFunc
Link to comment
Share on other sites

first of all thanks a lot for your help and code.

can you please do a favor for me?

this is the main app

Posted Image

on clicking \"Add New Path\" i want two buttons as shown in image. on clicking \"Browse Exe Files will open a window for selecting the file and on select it will add the path in its input and on clicking ok it will add it to the ini file.

Posted Image

i tried to add new line but it is not adding new line and shows something like this.

Posted Image

can you help me out? sorry for too much question (Newbie here)

if you need any more info the please ask...

i am creating a program which blocks restricted programs from running using ini files and have some other utalities. i tried to learn about ini files but could not.

thanks once again...

Link to comment
Share on other sites

HiSoftDev

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

$hGUI = GUICreate("Test", 300, 200)

$hListView = GUICtrlCreateListView("#|Program name|Program path", 10, 10, 280, 150)

$OkButton = GUICtrlCreateButton("Ok", 50, 170, 75, 23)

$AddButton = GUICtrlCreateButton("Add new path", 130, 170, 75, 23)

$RemButton = GUICtrlCreateButton("Remove", 210, 170, 75, 23)

$AddGUI = GUICreate("Add new path", 300, 100, -1, -1, -1, -1, $hGUI)

$input = GUICtrlCreateInput("", 10, 20, 280, 20)

$Browse_Button = GUICtrlCreateButton("Browse exe files", 10, 60, 85, 23)

$AddStr_Button = GUICtrlCreateButton("OK", 130, 60, 75, 23)

$Close_Button = GUICtrlCreateButton("Close", 215, 60, 75, 23)

GUISetState(@SW_SHOW, $hGUI)

_FillList(@ScriptDir & "\test.ini")

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $GUI_EVENT_CLOSE, $Close_Button
            If $msg[1] = $hGUI Then
                Exit
            Else
                GUISetState(@SW_ENABLE, $hGUI)
                GUISetState(@SW_HIDE, $AddGUI)
            EndIf
        Case $OkButton
            $sData = ""
            For $i = 0 To _GUICtrlListView_GetItemCount($hListView) - 1
                $sData &= _GUICtrlListView_GetItemText($hListView, $i, 2) & _GUICtrlListView_GetItemText($hListView, $i, 1) & @CRLF
            Next
            
            $hFile = FileOpen(@ScriptDir & "\test.ini", 2)
            FileWrite($hFile, $sData)
            FileClose($hFile)
            Exit
        Case $AddButton
            GUICtrlSetData($input, "")
            
            GUISetState(@SW_DISABLE, $hGUI)
            GUISetState(@SW_SHOW, $AddGUI)
        Case $RemButton
            _GUICtrlListView_DeleteItemsSelected($hListView)
        Case $Browse_Button
            $sFile = FileOpenDialog("Select File...", "", "Executable (*.exe)", 1, "", $AddGUI)
            If $sFile = "" Then ContinueCase
            
            GUICtrlSetData($input, $sFile)
        Case $AddStr_Button
            $sString = GUICtrlRead($input)
            If $sString = "" Then ContinueCase
            
            $sFile = StringRegExpReplace($sString, "^.*\\", "")
            $sPath = StringRegExpReplace($sString, "(^.*\\).*", "\1")
            
            GUICtrlCreateListViewItem(_GUICtrlListView_GetItemCount($hListView) + 1 & "|" & $sFile & "|" & $sPath, $hListView)
            
            GUISetState(@SW_ENABLE, $hGUI)
            GUISetState(@SW_HIDE, $AddGUI)
    EndSwitch
WEnd

Func _FillList($sFile)
    Local $iRead = FileRead($sFile)
    Local $aString = StringSplit(StringStripCR($iRead), @LF)
    
    For $i = 1 To $aString[0]
        If $aString[$i] = "" Then ContinueLoop
        GUICtrlCreateListViewItem($i & "|" & StringRegExpReplace($aString[$i], "^.*\\", "") & "|" & _
                                  StringRegExpReplace($aString[$i], "(^.*\\).*", "\1"), $hListView)
    Next
EndFunc
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...