Jump to content
Jibsbrown

Help with Loop overwriting ini

Recommended Posts

Jibsbrown

Hope someone can find what I've done wrong, because I have no idea. I'm sure its very simple fix.

Currently after selecting the folder for files the Listview the path is recorded in the INI file this which works. After submitting the ListView changes with the loop the INI file is over written and the file section name is not being written.

Any idea or examples for the solution? Forma searches and help file have not provided the answer(s). Or I'm just not seeing my mistake, which I'm sure is the real problem.

#RequireAdmin

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
; *** End added by AutoIt3Wrapper ***
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <ListViewConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GUIListViewEx.au3>

;User choses folder path
Global $Path = FileSelectFolder("Select Upadates Folder", @ScriptDir)

$upsPath = $Path
;MsgBox(0, "", $Path, 5)
$fUpsList = "InstallList.ini"
FileOpen($fUpsList)
IniWriteSection($fUpsList, "Path", $Path)
FileClose($fUpsList)
Global $aUpdates = _FileListToArrayRec($Path, "*.*", $FLTAR_FILES) ; All files in folder loaded into array for LV
$sFileCount = $aUpdates[0] ; Use file count for error checking - not added yet 3/29
ConsoleWrite($sFileCount & @CRLF)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 482, -1, -1)
$Group1 = GUICtrlCreateGroup("Group1", 40, 24, 400, 377)
$List1 = GUICtrlCreateListView("Update Files", 48, 72, 385, 305)
_GUICtrlListView_SetColumnWidth($List1, 0, 385)
$Submit = GUICtrlCreateButton("Submit", 400, 424, 75, 25)
$Exit = GUICtrlCreateButton("Exit", 504, 424, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

For $i = 1 To UBound($aUpdates) - 1
    GUICtrlCreateListViewItem($aUpdates[$i], $List1)
Next

GUISetState()

$lUps = _GUIListViewEx_Init($List1, $aUpdates, 0, 0, True)

_GUIListViewEx_MsgRegister()

While 1
    $vRet = _GUIListViewEx_EventMonitor() ; Reread GLVEx Help doc
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Error", "Event error: " & @error)
    EndIf
    Switch @extended
        Case 0

        Case 4


    EndSwitch
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE

            Exit
        Case $Submit

            $aContent = _GUIListViewEx_ReturnArray($lUps)
            _FileWriteFromArray($fUpsList, $aContent)
            ;this $error check returns 5 - Start index is greater than the $iUbound parameter
            ;So if the list is empty, it returns error 5
            If @error = 5 Then


                FileOpen($fUpsList)
                IniWriteSection($fUpsList, "InstallOrder", "")
                FileClose($fUpsList)

            EndIf
            Exit
        Case $Exit
            Exit
    EndSwitch


WEnd

 

Share this post


Link to post
Share on other sites
Jos

What are the FileOpen and Fileclose suppose to do here? They serve no purpose other than eating up filehandle  space.

                FileOpen($fUpsList)
                IniWriteSection($fUpsList, "InstallOrder", "")
                FileClose($fUpsList)

Also change this line to something line (assuming the ini file is located in the scriptdir:

$fUpsList = @scriptdir & "\InstallList.ini"

Else when the workdir would change, it will write to the wrong place. 

As to the overwriting: In the helpfile this is stated for _FileListToArrayRec()

Quote

Remarks

If a string path is provided, the file will be overwritten and closed.
To use other write modes, like append or Unicode formats, open the file with FileOpen() first and pass the file handle instead.
If a file handle is passed, the file will still be open after writing.

.. and you are providing the filename, not a filehandle.

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Jibsbrown

Thank you Jos for the correction, but even without 

FileOpen($fUpsList)
                IniWriteSection($fUpsList, "InstallOrder", "")
                FileClose($fUpsList)

Still getting the same result and the above was a stab at thinking I did not know what I was doing (which I'm positive of since I'm a 90 day Autoit Noob).

So with a little playing around I did get the file path to write but its showing up in a file titled "1" without an extension. With the following line added...

$ActiveFile = FileOpen($fUpsList, $FO_APPEND)
IniWriteSection($ActiveFile, "Path", $upsPath)

Without these lines still getting the same results without the FileOpen() / FileClose. The $path is written but the loop overwrites the ini file and only records the LV afte the submit button is pressed.

#RequireAdmin

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
; *** End added by AutoIt3Wrapper ***
#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <ListViewConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GUIListViewEx.au3>

;User choses folder path
Global $Path = FileSelectFolder("Select Upadates Folder", @ScriptDir)

$upsPath = $Path
;MsgBox(0, "", $Path, 5)
$fUpsList = @ScriptDir & "\InstallList.ini"
;$ActiveFile = FileOpen($fUpsList, $FO_APPEND)
IniWriteSection($fUpsList, "Path", $upsPath)

Global $aUpdates = _FileListToArrayRec($Path, "*.*", $FLTAR_FILES) ; All files in folder loaded into array for LV
$sFileCount = $aUpdates[0] ; Use file count for error checking - not added yet 3/29
ConsoleWrite($sFileCount & @CRLF)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 482, -1, -1)
$Group1 = GUICtrlCreateGroup("Group1", 40, 24, 400, 377)
$List1 = GUICtrlCreateListView("Update Files", 48, 72, 385, 305)
_GUICtrlListView_SetColumnWidth($List1, 0, 385)
$Submit = GUICtrlCreateButton("Submit", 400, 424, 75, 25)
$Exit = GUICtrlCreateButton("Exit", 504, 424, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

For $i = 1 To UBound($aUpdates) - 1
    GUICtrlCreateListViewItem($aUpdates[$i], $List1)
Next

GUISetState()

$lUps = _GUIListViewEx_Init($List1, $aUpdates, 0, 0, True)

_GUIListViewEx_MsgRegister()

While 1
    $vRet = _GUIListViewEx_EventMonitor() ; Reread GLVEx Help doc
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Error", "Event error: " & @error)
    EndIf
    Switch @extended
        Case 0

        Case 4


    EndSwitch
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE

            Exit
        Case $Submit

            $aContent = _GUIListViewEx_ReturnArray($lUps)
            _FileWriteFromArray($fUpsList, $aContent)
            ;this $error check returns 5 - Start index is greater than the $iUbound parameter
            ;So if the list is empty, it returns error 5
            If @error = 5 Then

                IniWriteSection($fUpsList, "InstallOrder", "")

            EndIf
            Exit
        Case $Exit
            Exit
    EndSwitch
    ;FileClose($fUpsList)

WEnd

And side note... Currently using Autoit Ver 3.3.14.5 and the "Remarks" for don't mention what you posted about "Remarks".

image.thumb.png.e7f74f6a57af41ac5d335bc28cd238ae.png

But fairly sure I'm not putting "1+1" together correctly.

Share this post


Link to post
Share on other sites
Jos

Just totally forget about the FileOpen() / FileClose for IniWriteSection as it surves no purpose.
The last part of my post is probably the reason why you overwrite the file but I goofed up and Copy/pasted the wrong command. It obviously should have been  _FileWriteFromArray($fUpsList, $aContent)  ( Sorry about that :) )

13 hours ago, Jos said:

As to the overwriting: In the helpfile this is stated for _FileListToArrayRec() _FileWriteFromArray()

Quote

Remarks

If a string path is provided, the file will be overwritten and closed.
To use other write modes, like append or Unicode formats, open the file with FileOpen() first and pass the file handle instead.
If a file handle is passed, the file will still be open after writing.

.. and you are providing the filename, not a filehandle.

Check that out and try using FileOpen() and using the returned handle with  _FileWriteFromArray().

Jos

 


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Subz

Your output doesn't conform to ini standards, it should be something like:

#RequireAdmin

#include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <GUIListViewEx.au3>

Global $fUpsList = @ScriptDir &  "\InstallList.ini"
Global $sFilePath = FileSelectFolder("Select Upadates Folder", @ScriptDir)
    IniWrite($fUpsList, "Updates Path", "Path", $sFilePath)
Global $aUpdates = _FileListToArrayRec($sFilePath, "*.*", $FLTAR_FILES) ; All files in folder loaded into array for LV
    If @error Then Exit MsgBox(16, "Error", "No files found")
    _ArrayDelete($aUpdates, 0) ;~ Delete the first row (count) for imporing into ListView
    ;~ Turn 1D Array into 2D Array $aUpdates[1][0] for importing into ListView
    _ArrayColInsert($aUpdates, 1)
    _ArrayColDelete($aUpdates, 1)

    _UpdateList()

Func _UpdateList()
    Local $vRet, $aContent
    Local $hMainForm = GUICreate("Form1", 615, 482, -1, -1)
    Local $idGroup1 = GUICtrlCreateGroup("Group1", 40, 24, 400, 377)
    Local $idListView = GUICtrlCreateListView("Update Files", 48, 72, 385, 305)
        _GUICtrlListView_SetColumnWidth($idListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
        _GUICtrlListView_AddArray($idListView, $aUpdates)
    Local $idSubmit = GUICtrlCreateButton("Submit", 400, 424, 75, 25)
    Local $idExit = GUICtrlCreateButton("Exit", 504, 424, 75, 25)
    GUISetState(@SW_SHOW)

    GUISetState()

    $lUps = _GUIListViewEx_Init($idListView, $aUpdates, 0, 0, True)
    _GUIListViewEx_MsgRegister()

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE, $idExit
                Exit
            Case $idSubmit
                ;~ Clear the Updates List
                IniDelete($fUpsList, "Updates")
                $aContent = _GUIListViewEx_ReturnArray($lUps)
                For $i = 0 To UBound($aContent) - 1
                    IniWrite($fUpsList, "Updates", "File" & $i, $aContent[$i][0])
                Next
                Exit
        EndSwitch
    WEnd
EndFunc

 

Share this post


Link to post
Share on other sites
Jibsbrown

Just wanted to thank Jos and Subz. You both provided me some work to do... will update in 48 hours with what I find from both posts.

Share this post


Link to post
Share on other sites
Jibsbrown

Sorry for taking longer then 48 hours to reply, but this has lead to a rabbit hole of coding challenges. 

Jos's: 

I was able to fix the my problems with FileOpen() but still had to rewrite to get iniwritesection to get script to fully write the ini file correctly.

...after looking and working with Subz's post (it cut my code lines by 20).

And with Subz pointing out that... 

_ArrayColDelete($aUpdates, 1)

existed... very easy solution for displaying only one column for an array in list view. 

it saved me from trying to figure out how to work one of Zedna's solutions (which provided a 20 day Autoit Noob so many challenges) into the work project.

 

Thank you both for the help and will post the end results in a few days for your review... if this post is still open

but if not, will post when scripted complete. Thanks for the help and pointers. 

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

  • Similar Content

    • VollachR
      By VollachR
      Hi,
      I'm creating a tool to edit some ini files that belong to a program I'm working on...
      Basically everything work fine except I can't find a way to check if the if the WriteIniSection operation was successful...
      For Example, let's say the INI file is by mistake or some other reason Read Only, or the user don't have the right permission to write to the file or the folder it's in... Right now, in such a case The INI file, as expected, won't get updated, but I when I perform an if error or if not error checks I always get the msgbox for the if not error.
      Here's the relevant code part:
       
      IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Conversion", $aConversion, 1) IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "AIO", $aAIO, 1) IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Data", $aData, 1) IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Redist", $aRedist, 1) IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Split", $aSplit, 1) IniWriteSection(@ScriptDir & "\Settings\Settings.ini", "Autorun", $aAutorun, 1) If Not @error Then MsgBox($MB_ICONINFORMATION, "Success", "Settings.ini Saved Successfully") Else If @error Then MsgBox($MB_ICONERROR, "Failure", "Settings.ini Could Not Be Saved!" & @CRLF & @CRLF & "Please Check If The File Is ReadOnly And That You Have Permission To Change It Or Its Location") EndIf EndIf  
      How can I perform a check to see if the ini file was written to successfully?
      Thanks.
    • JonBMN
      By JonBMN
      My first question before I start posting code and whatnot, is on the IniWriteSection the Failure returns 0 and sets @error to 1 for an invalid data type. Can anyone help me figure out what this certain invalid data type is, because the helpfile does not explain this is huge detail (I'm passing a 2d array with key value pairs).
    • hackersarchangel
      By hackersarchangel
      Ok so here's my "evil plan" lol: I am working on making a test to help me study for the A+. The idea is that I want to make it modular so I can specify a battery of tests.

      My bright idea was to make an ini file generator, since that has built functions and a method for sorting. The generator is mostly done, it's a basic one to make multiple choice questions. Here's the tricky bit: I want to be able to mark each answer with 1, 2, 3, and 4 so I can then have the test program grab the answers at random, but still relevant to each question, and not show the numbers so it's harder to tell which ones are which.

      Example:

      Q. Who founded Apple Computer, Inc?
      1) Steve Ballmer
      2)Steve Jobs and Steve Wozniak
      3) Bill Gates and Steve Jobs
      4) Tom Petty and the Heartbreakers

      but have that be in a different order everytime I take the test, so I can't cheat even though I'm writing the test lol. I have that figured out as far as how it's organized with the questions, just need to figure out how to grab the questions and answers, but do it in a way that it's not seen in the test, so I can randomize them. So far I'm sure I can randomize the question order, but still not sure on how to keep it from repeating a question.

      Here's my code so far for the ini generator, the Test portion I'll be working on as soon as I can think of a way to have it pick a question at random. Partially why I'm here


      #include <GUIConstantsEx.au3> ;Constants for GUI Events #include <WindowsConstants.au3> Global $Answer, $Answer_input[4], $counter = 0, $Build_bttn, $Question, $Question_input _Create_GUI() Func _Create_GUI() ;To implement a reset function, make everything in the GUI first, THEN put all the case stuff inside ;_Main(). HotKeySet("{F3}","_Main") ;A program reset switch, in case a function gets funky and doesn't process right. GUICreate("Question and Answer Generator Version 0.1",640,480) ;The code for adding questions and answers to the .ini file. $Question_input = GUICtrlCreateEdit("Question goes here",45,45, 250, 75) $Answer_input[0] = GUICtrlCreateInput("1)", 45, 150, 300, 20) $Answer_input[1] = GUICtrlCreateInput("2)", 45, 175, 300, 20) $Answer_input[2] = GUICtrlCreateInput("3)", 45, 200, 300, 20) $Answer_input[3] = GUICtrlCreateInput("4)", 45, 225, 300, 20) $Build_bttn = GUICtrlCreateButton("Add to File", 525,440) _Main() EndFunc Func _Main() GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $Gui_Event_Close Exit Case $Build_bttn $Question = GUICtrlRead($Question_input) $Answer = GUICtrlRead($Answer_input[0]) & @LF & GUICtrlRead($Answer_input[1]) & @LF & GUICtrlRead($Answer_input[2]) & @LF & GUICtrlRead($Answer_input[3]) If ($counter) < 10 Then $counter = $counter+1 IniWrite("AlphaTest.ini","Questions","Question_" & $counter, $Question) IniWriteSection("AlphaTest.ini","Answer_" & $counter,$Answer) EndIf EndSwitch WEnd EndFunc
      I understand this is a complicated project, one I'm willing to share back with the community of course since the idea is to make it a simple generator and then just point the Test exe to the q&a file
×