Jump to content

ini file path


Recommended Posts

Hi,

Is there a way to set the path for a ini so that it can be updated or changed without hard coding in the script and having to re-compile it.

Example code:

$iniFile = @ScriptDir & "\IniReadWriteDeleteRenameTest.ini"

If FileExists($iniFile) Then
    ;File Exist do nothing
    ;MsgBox(4096, "C:\autoexec.bat File", "Exists")
Else
    ;File does not exist then create
    ;MsgBox(4096,"C:\autoexec.bat File", "Does NOT exists")
    
    $file = FileOpen($iniFile, 1)

    ; Check if file opened for writing OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    FileWrite($file, "")    
    FileClose($file)    
EndIf


_iniDeleteSection()
_iniDeleteKey()
_iniRead()
_iniReadSection()
_iniReadSectionNames()
_iniRenameSection()
_iniWrite()
_iniWriteSection()

Func _iniDeleteSection()
    IniDelete($iniFile, "config")
EndFunc

Func _iniDeleteKey()
    IniDelete($iniFile, "Programs", "Type1")
EndFunc

Func _iniRead()
    $var = IniRead($iniFile, "Programs", "Type1", "NotFound")
    MsgBox(4096, "Result", $var)
EndFunc

Func _iniReadSection()
    $var = IniReadSection($iniFile, "Programs")
    If @error Then 
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    Else
        For $i = 1 To $var[0][0]
            MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
        Next
    EndIf
EndFunc

Func _iniReadSectionNames()
    $var = IniReadSectionNames($iniFile)
    If @error Then 
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    Else
        For $i = 1 To $var[0]
            MsgBox(4096, "", "Section Name: " & $var[$i])
        Next
    EndIf
EndFunc

Func _iniRenameSection()    
    $res = IniRenameSection($iniFile, "Setup Programs", "Programs Setup")
EndFunc


Func _iniWrite() 
    IniWrite($iniFile, "Install Programs", "status3", "Check")
EndFunc

Func _iniWriteSection()
    ; This is the INI file we will write to.  It will be created on the Desktop.
    $sIni = @DesktopDir & "\AutoIt-Test.ini"

    ; Demonstrate creating a new section using a string as input.
    $sData = "Key1=Value1" & @LF & "Key2=Value2" & @LF & "Key3=Value3"
    IniWriteSection($sIni, "Section1", $sData)

    ; Demonstrate creating a new section using an array as input.
    $aData1 = IniReadSection($sIni, "Section1") ; Read in what we just wrote above.
    For $i = 1 To UBound($aData1) - 1
        $aData1[$i][1] &= "-" & $i  ; Change the data some
    Next

    IniWriteSection($sIni, "Section2", $aData1) ; Write to a new section.

    ; Demonstrate creating an array manually and using it as input.
    Dim $aData2[3][2] = [ [ "FirstKey", "FirstValue" ], [ "SecondKey", "SecondValue" ], [ "ThirdKey", "ThirdValue" ] ]
    ; Since the array we made starts at element 0, we need to tell IniWriteSection() to start writing from element 0.
    IniWriteSection($sIni, "Section3", $aData2, 0)
EndFunc

Func _iniRename()

EndFunc

Thank you for your help,

jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Link to comment
Share on other sites

I don't understand. In your example the ini is in the same folder, so no extra work is needed.

Inverted,

The posted code is an example. I have another longer code I will insert this feature in if it is possible.

I need to be able to change the path of the ini file after it is compiled when using it with other selected files. Is there a way to set the path of the ini file without hard coding it in the code like the example has it?

Thank you for your help,

jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Link to comment
Share on other sites

Well, the compiled proggy will have to read it from somewhere. Either pass the full ini path as a parameter (you can also drag'n'drop the ini file to your compiled script, windows will pass it as a parameter), or read it from the registry, read it from another ini file (lol), use FileOpen.

Your example looks fine, you check the same directory for the existance of the script, and use FileOpen if it's not there, nothing is hardcoded, except the ini filename. You could use the @Workingdir macro !

Edited by Inverted
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...