BetaLeaf

[Solved]IniWriteSection Returning 0 but no @error

12 posts in this topic

#1 ·  Posted (edited)

So what im trying to do is overwrite values from one ini file to another.  The destination ini file would have its values replaced by the values read by a source ini file.

Can anyone spot the issue?

Here is the code in question: 

#include "date.au3"
IniProcessor("Source.ini", "Dest.ini")
Func IniProcessor($ModFile, $IniToMod)
    Local $sectionData = IniReadSectionNames($ModFile)
    If BackupIni($IniToMod) = 1 Then
        For $i = 1 To $sectionData[0]
            Local $iArray = IniReadSection($ModFile, $sectionData[$i])
            $WriteResult = IniWriteSection($IniToMod, $sectionData[$i], IniReadSection($ModFile, $sectionData[$i]))
            ;If $WriteResult = 0 Then MsgBox(16, "Error", "Error writing Ini File." & @CRLF & "Code: " & @error & @CRLF)
        Next
    EndIf
    MsgBox(64, "IniProcessor", "Done.")
    Exit
EndFunc   ;==>IniProcessor
Func BackupIni($IniToBackup)
    $sysTime = _Date_Time_GetSystemTime()
    $sysTime = _Date_Time_SystemTimeToDateTimeStr($sysTime)
    $BackUpFile = $IniToBackup & ".backup_" & $sysTime
    $BackUpFile = StringReplace($BackUpFile, "/", "-")
    $BackUpFile = StringReplace($BackUpFile, ":", "-")
    $CopyResult = FileCopy($IniToBackup, $BackUpFile)
    If $CopyResult = 1 Then Return 1
    If $CopyResult = 0 Then Return 0
EndFunc   ;==>BackupIni

An Example Source.ini

[Section1]
Name1=Value1

An Example Dest.ini

[Section1]
Name1=Value2

When this is run, Dest.ini should read

[Section1]
Name1=Value1

 

Edited by BetaLeaf

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites



BetaLeaf,

IniReadSection does not return an array that can be used to feed IniWriteSection.  See the Help file.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I did see the Help file and it says:

Success: a 2 dimensional array where element[n][0] is the key and element[n][1] is the value.
Failure: sets the @error flag to non-zero if unable to read the section (The INI file may not exist or the section may not exist or is empty)

It does indeed return an array. I can even use _ArrayDisplay to read it. Maybe you're the one that needs to see the Help file?

Edited by BetaLeaf

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

Ok I found the issue. The Destination Ini I was trying to write to was read only and thus why it failed.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

BetaLeaf,

Hmmm, tested this as follows...

local $ini1 = @scriptdir & '\initest1.ini'
local $ini2 = @scriptdir & '\initest2.ini'

local $ret = iniwritesection($ini2,'cars',inireadsection($ini1,'cars'))

ConsoleWrite($ret & @CRLF)

initest2.ini

initest1.ini

The code runs successfully so my assertion is wrong.  Note, a return of 1 means success.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Excellent, apologies for the mis-information...

2 people like this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

No problem. Mistakes happen to the best of us. B)

1 person likes this

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

I was wondering why it was failing and also why you read the same section twice, but don't use the $iArray from the first read.

Local $iArray = IniReadSection($ModFile, $sectionData[$i])

$WriteResult = IniWriteSection($IniToMod, $sectionData[$i], IniReadSection($ModFile, $sectionData[$i]))

Wouldn't it just be better to use

$WriteResult = IniWriteSection($IniToMod, $sectionData[$i], $iArray)

Or was that just something left over from testing.


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

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

It was left over from debugging. My current issue is now getting the iniwritesection to not overwrite values its not reading from source.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

I think you will find it is replacing the whole section, and your only way around that, is with a loop and writing each key/value pair separately from the read array.


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

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

yup I already figured that one out @TheSaint. I'm working on the for next loop as we speak.


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Here is the completed code in case anyone wants it.

$source = FileOpenDialog("Select Mod Ini file - IniProcessor", "", "Ini Settings File (*.ini)")
If @error Then Exit
ConsoleWrite("Source="&$source&@CRLF)
$dest = FileOpenDialog("Select Ini file to Mod - IniProcessor", "", "Ini Settings File (*.ini)")
If @error Then Exit
ConsoleWrite("dest="&$dest&@CRLF)
IniProcessor($source, $dest)
Func IniProcessor($ModFile, $IniToMod)
    FileSetAttrib($ModFile, "-R")
    Local $sectionData = IniReadSectionNames($ModFile)
    If BackupIni($IniToMod) = 1 Then
        For $i = 1 To $sectionData[0]
            Local $iArray = IniReadSection($ModFile, $sectionData[$i])
            For $j = 1 To UBound($iArray) - 1
                $section = $sectionData[$i]
                $Name = $iArray[$j][0]
                $Value = $iArray[$j][1]
                $WriteResult = IniWrite($IniToMod, $section, $Name, $Value)
                If $WriteResult = 0 Then MsgBox(16, "Error", "Error writing Ini File." & @CRLF & "Code: " & @error & @CRLF)
                ConsoleWrite("IniWrite(" & $IniToMod & "," & $section & "," & $Name & "," & $Value & ")" & @CRLF)
            Next
        Next
    EndIf
    MsgBox(64, "IniProcessor", "Done.")
    Exit
EndFunc   ;==>IniProcessor
Func BackupIni($IniToBackup)
    $sysTime = @MON&"-"&@MDAY&"-"&@YEAR&"_"&@HOUR&"."&@MIN&"."&@SEC
    $BackUpFile = $IniToBackup & ".backup_" & $sysTime
    $CopyResult = FileCopy($IniToBackup, $BackUpFile)
    ConsoleWrite("FileCopy("&$IniToBackup&","& $BackUpFile&")")

    If $CopyResult = 1 Then Return 1
    If $CopyResult = 0 Then
        MsgBox(16, "Error", "Failed to create backup.")
        Return 0
    EndIf
EndFunc   ;==>BackupIni

Please use the source and dest ini from the original post for references if necessary.

Edited by BetaLeaf
Fixed an issue with backing up the ini

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

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