Jump to content

INI to List


Recommended Posts

I cannot get a simple INI file to print out to a list GUI. Can someone point me in the right direction?

Whenever I try using the example below, nothing prints to the list.

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <GUIConstants.au3>
#Include <Constants.au3>


$Form1 = GUICreate("AForm1", 633, 447, 193, 115)
$List1 = GUICtrlCreateList("", 40, 40, 169, 357)
GUISetState(@SW_SHOW)
$FileList = IniReadSection("test.ini","Test")

For $i In $FileList
    GUICtrlSetData($List1,$FileList)
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
[Test]
Test=0
Test1=1
Test2=2

I am still trying to understand the array code so i wouldn't be surprised if I am doing it wrong.

The point is only to have whats in the INI populated into the list; no need for counting. 

--------------------------------------------------------------------------------------------------------------------------------------

For anyone that finds this thread in search of similar answers, here was the solution:

#include <Array.au3>
#Include <File.au3>
#Include <GUIConstants.au3>
#include <GuiListBox.au3>
#include <ListBoxConstants.au3>

Local $hGui = GUICreate("AForm1", 633, 447, 193, 115)
;~ Sorted List
GUICtrlCreateLabel("Sorted List", 40, 15, 170, 20)
GUICtrlSetFont(-1, 14, 400)
Local $idList1 = GUICtrlCreateList("", 40, 40, 170, 357)
;~ Unsorted List
GUICtrlCreateLabel("Unsorted List", 220, 15, 170, 20)
GUICtrlSetFont(-1, 14, 400)
Local $idList2 = GUICtrlCreateList("", 220, 40, 169, 357, BitOR($LBS_NOTIFY, $WS_VSCROLL, $WS_BORDER))
GUICtrlSetFont(-1, 12, 400)
GUICtrlSetColor(-1, 0xFF3300)
GUISetState()
Local $aFileList
_FileReadToArray("Test.txt", $aFileList)
For $i = 1 To $aFileList[0]
     _GUICtrlListBox_AddString($idList1,$aFileList[$i])
     _GUICtrlListBox_AddString($idList2,$aFileList[$i])
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

These were the lines responsible for producing the final desired outcome in my case:


Local $aFileList
_FileReadToArray("Test.txt", $aFileList)
For $i = 1 To $aFileList[0]
     _GUICtrlListBox_AddString($idList1,$aFileList[$i])
     _GUICtrlListBox_AddString($idList2,$aFileList[$i])
Next

 

Edited by Daemante2018
Link to comment
Share on other sites

INIReadSection returns a 2D array, so you can't do it that way, you'd have to loop through the array instead.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I'd say is the easiest

For $i=1 To $FileList[0][0]
    GUICtrlSetData($List1,$FileList[$i][1])
Next

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

5 minutes ago, careca said:

I'd say is the easiest

For $i=1 To $FileList[0][0]
    GUICtrlSetData($List1,$FileList[$i][1])
Next

 

Yes so from that code it would appear I don't know how to use arrays. 

That worked.

 

Using that example, the result was the "values" in the INI file. Should it not return the whole section?

Edited by Daemante2018
Link to comment
Share on other sites

How could I change this example to use FileRead and a TXT file instead to have it read/format it line by line? 

This reads the information but it does not format line by line:

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
#Include <GUIConstants.au3>
#Include <Constants.au3>


$Form1 = GUICreate("AForm1", 633, 447, 193, 115)
$List1 = GUICtrlCreateList("", 40, 40, 169, 357)
GUISetState(@SW_SHOW)
$FileList = FileRead("test.txt")
GUICtrlSetData($List1, $FileList)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

 

Link to comment
Share on other sites

No you would have to use something like _FileReadToArray example:

#Include <File.au3>
#Include <GUIConstants.au3>

Local $hGui = GUICreate("AForm1", 633, 447, 193, 115)
Local $idList = GUICtrlCreateList("", 40, 40, 169, 357)
GUISetState()
Local $aFileList
_FileReadToArray("Test.txt", $aFileList)

For $i = 1 To $aFileList[0]
    GUICtrlSetData($idList,$aFileList[$i])
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Link to comment
Share on other sites

2 minutes ago, Subz said:

No you would have to use something like _FileReadToArray example:

#Include <File.au3>
#Include <GUIConstants.au3>

Local $hGui = GUICreate("AForm1", 633, 447, 193, 115)
Local $idList = GUICtrlCreateList("", 40, 40, 169, 357)
GUISetState()
Local $aFileList
_FileReadToArray("Test.txt", $aFileList)

For $i = 1 To $aFileList[0]
    GUICtrlSetData($idList,$aFileList[$i])
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

I was actually playing with Filereadtoarray but I couldn't get it working; as opposed to _FileReadToArray. Your example worked as expected. 

Thank you.

Edited by Daemante2018
Link to comment
Share on other sites

Can you just explain this one part of the array:

For $i = 1 To $aFileList[0] <-------
    GUICtrlSetData($idList,$aFileList[$i])
Next

What does the [0] represent? Array type = 1D?

Also noticed that it will only read unique strings from the file and print those. Anything that is duplicate is left out. Is that behavior changeable?

 

Is there anyway to have the file formatted, such as in RTF form, and have that formatting carry over to the Listbox?

Edited by Daemante2018
Link to comment
Share on other sites

When you use _FileReadToArray without the optional parameters, the first item in the array or $aFileList[0] returns the last row number in the array, so

 For $i = 1 To <End of Array> see example below:

_FileReadToArray without the $sDelimiter will return a 1D array otherwise it will display a 2D array in which case you would need to use For $i = 1 To $aFileList[0][0]

With regards to RTF format, no I don't believe lists support that format.

#include <Array.au3>
#Include <File.au3>
#Include <GUIConstants.au3>

Local $hGui = GUICreate("AForm1", 633, 447, 193, 115)
Local $idList = GUICtrlCreateList("", 40, 40, 169, 357)
GUISetState()
Local $aFileList
_FileReadToArray("Test.txt", $aFileList)
_ArrayDisplay($aFileList)
For $i = 1 To $aFileList[0]
    GUICtrlSetData($idList,$aFileList[$i])
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Link to comment
Share on other sites

15 hours ago, Subz said:

When you use _FileReadToArray without the optional parameters, the first item in the array or $aFileList[0] returns the last row number in the array, so

 For $i = 1 To <End of Array> see example below:

_FileReadToArray without the $sDelimiter will return a 1D array otherwise it will display a 2D array in which case you would need to use For $i = 1 To $aFileList[0][0]

With regards to RTF format, no I don't believe lists support that format.

#include <Array.au3>
#Include <File.au3>
#Include <GUIConstants.au3>

Local $hGui = GUICreate("AForm1", 633, 447, 193, 115)
Local $idList = GUICtrlCreateList("", 40, 40, 169, 357)
GUISetState()
Local $aFileList
_FileReadToArray("Test.txt", $aFileList)
_ArrayDisplay($aFileList)
For $i = 1 To $aFileList[0]
    GUICtrlSetData($idList,$aFileList[$i])
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Thanks again for further explaining the concept @Subz!

Link to comment
Share on other sites

_ArrayUnique

Doesn't _FileReadToArray return all the lines for you?

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

If I use a txt file like below it only returns unique lines:

 

test
test1
test2
test3
test4
test
test1
test2
test3
test4
test
test1
test2
test3
test4
test
test1
test2
test3
test4
test
test1
test2
test3

_ArrayUnique is meant to remove the duplicates, from what I read in the help file, but I would like to leave the duplicates in.

 

Edited by Daemante2018
Link to comment
Share on other sites

You mean something like:

#include <Array.au3>
#Include <File.au3>
#Include <GUIConstants.au3>
#include <GuiListBox.au3>
#include <ListBoxConstants.au3>

Local $hGui = GUICreate("AForm1", 633, 447, 193, 115)
;~ Sorted List
GUICtrlCreateLabel("Sorted List", 40, 15, 170, 20)
GUICtrlSetFont(-1, 14, 400)
Local $idList1 = GUICtrlCreateList("", 40, 40, 170, 357)
;~ Unsorted List
GUICtrlCreateLabel("Unsorted List", 220, 15, 170, 20)
GUICtrlSetFont(-1, 14, 400)
Local $idList2 = GUICtrlCreateList("", 220, 40, 169, 357, BitOR($LBS_NOTIFY, $WS_VSCROLL, $WS_BORDER))
GUICtrlSetFont(-1, 12, 400)
GUICtrlSetColor(-1, 0xFF3300)
GUISetState()
Local $aFileList
_FileReadToArray("Test.txt", $aFileList)
For $i = 1 To $aFileList[0]
     _GUICtrlListBox_AddString($idList1,$aFileList[$i])
     _GUICtrlListBox_AddString($idList2,$aFileList[$i])
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

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