Jump to content

Help with Loop overwriting ini


Recommended Posts

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

 

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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. 

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

×
×
  • Create New...