Jump to content

INI2AU3


sugi
 Share

Recommended Posts

I sometimes have to create INI files and made a small script to do so. Since .INI files are usually small I didn't bother to use Filehandles.

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       USCv3
;
Break(0)
AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("TrayIconHide", 0)
AutoItSetOption("TrayIconDebug", 1)

Dim $INI, $AU3, $Line = '', $LineCounter = 1, $Section = '', $Key = '', $Value = ''

$INI = FileOpenDialog('Choose .INI file', '.', 'INI-Files (*.ini)', 1 + 2)
If @error Then Exit (1)

$AU3 = FileSaveDialog('Choose output .AU3 file', '.', 'AU3-Files (*.au3)', 2 + 16)
If @error Then Exit (1)

If FileExists($AU3) Then FileDelete($AU3)
If @error Then ThrowError('Error', 'Cannot delete ' & $AU3)

FileWriteLine($AU3, '$INI="file.ini"')

SetError(0)
$Line = FileReadLine($INI, $LineCounter)
While NOT @error
    $Line = StringStripWS($Line, 1)
    Select
        Case $Line = ''
        Case StringLeft($Line, 1) = '['
            If StringInStr($Line, ']', 0, -1) = 0 Then ThrowError('Parsing error', 'Line ' & $LineCounter & ': missing closing bracket')
            $Section = StringMid($Line, 2, StringInStr($Line, ']', 0, -1) - 2)
        Case StringInStr($Line, '=')
            $Key = StringLeft($Line, StringInStr($Line, '=') - 1)
            $Value = StringRight($Line, StringLen($Line) - StringInStr($Line, '='))
            If $Section = '' Then ThrowError('Parsing', 'Line ' & $LineCounter & ': missing section name')
            FileWriteLine($AU3, 'IniWrite($INI, "' & $Section & '", "' & $Key & '", "' & $Value & '")')
        Case Else
            ThrowError('Parsing', 'Line ' & $LineCounter & ': unknown line')
    EndSelect
    $LineCounter = $LineCounter + 1
    $Line = FileReadLine($INI, $LineCounter)
Wend

MsgBox(4096, 'Conversion finished', 'The script was written to ' & $AU3)

Func ThrowError($Title, $Text)
    MsgBox(4096, $Title, $Text)
    Exit (1)
EndFunc

EDIT: Small bugfix. Script crashed instead of showing an error when an unknown line was encountered.

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