Jump to content
Lisuter

How to read from txt file

Recommended Posts

Hello...

How i can write data from txt to +3 Input?

Example:

In txt i have:
Michael XYZ, 21, Boston
Olex Marshal, 17, Boston
Alex XYZ, 19, Boston

 

And now i want load that data to input in autoit. 

Name          y.o          city
[ Input1]  [Input2] [Input3]
[ Input4]  [Input5] [Input6]
[ Input7]  [Input8] [Input9]

 

After load should be:

Name               y.o            city
[Michael XYZ]      [21]     [Boston]
[ Olex Marshal]  [17]      [Boston]
[ Alex XYZ]           [19]      [Boston]

 

FileOpen("Test.txt",0)
            
            $aas = GUICtrlSetData($Input1, FileReadLine("Test.txt",1))
            FileClose("Test.txt")

But its wrong. ofcourse its load all to input 1. So now how do you load this data directly into each input separately? The comma stop sign is here. Each line is a new person

Edited by Lisuter

Share this post


Link to post
Share on other sites

Okay i try some and now working with first line from txt and load it to Input1, Input2, Input3 correctly.

Now, how to loop all, each line from txt to each input?

My working code for each:

    FileOpen("Test.txt",0)
            MsgBox(0,"Test", FileReadLine("Test.txt",1))
            $aas = FileReadLine("Test.txt",1)
            $aSplitLine = StringSplit($aas,",")
            GUICtrlSetData($Input1, $aSplitLine[1])
            GUICtrlSetData($Input2, $aSplitLine[2])
            GUICtrlSetData($Input3, $aSplitLine[3])
            FileClose("Test.txt")

 

Edited by Lisuter

Share this post


Link to post
Share on other sites

@Lisuter as an awareness, in the future - especially when dealing with a GUI - you will always receive more help if you post your entire code rather than asking us to guess at what you are trying to do.

That said, if you are trying to simply update the input fields on the GUI with a line from the text file, you could do something like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $aFile = FileReadToArray("test.txt")
Local $hGUI = GuiCreate("Test", 300, 200)
Local $Input1 = GUICtrlCreateInput("", 10, 10, 90, 50)
Local $Input2 = GUICtrlCreateInput("", 105, 10, 90, 50)
Local $Input3 = GUICtrlCreateInput("", 200, 10, 90, 50)
Local $sGo = GUICtrlCreateButton("Go", 250, 150, 40, 40)

GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $sGo
                For $sLine In $aFile
                    $aTemp = StringSplit($sLine, ",")
                        GUICtrlSetData($Input1, $aTemp[1])
                        GUICtrlSetData($Input2, $aTemp[2])
                        GUICtrlSetData($Input3, $aTemp[3])
                        Sleep(1000)
                Next
        EndSwitch
    WEnd

If you are looking rather to add all the lines into the input fields, you can do something like this:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $aFile = FileReadToArray("test.txt")
Local $hGUI = GuiCreate("Test", 300, 200)
Local $Input1 = GUICtrlCreateInput("", 10, 10, 90, 50, $ES_MULTILINE)
Local $Input2 = GUICtrlCreateInput("", 105, 10, 90, 50, $ES_MULTILINE)
Local $Input3 = GUICtrlCreateInput("", 200, 10, 90, 50, $ES_MULTILINE)
Local $sGo = GUICtrlCreateButton("Go", 250, 150, 40, 40)

GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $sGo
                For $sLine In $aFile
                    $aTemp = StringSplit($sLine, ",")
                        GUICtrlSetData($Input1, ((GUICtrlRead($Input1) = "") ? $aTemp[1] :GUICtrlRead($Input1) & @CRLF & $aTemp[1]))
                        GUICtrlSetData($Input2, ((GUICtrlRead($Input2) = "") ? $aTemp[2] :GUICtrlRead($Input2) & @CRLF & $aTemp[2]))
                        GUICtrlSetData($Input3, ((GUICtrlRead($Input3) = "") ? $aTemp[3] :GUICtrlRead($Input3) & @CRLF & $aTemp[3]))
                        Sleep(1000)
                Next
        EndSwitch
    WEnd

Beyond a couple of lines, however, you may want to switch to a listview for easier handling.

Share this post


Link to post
Share on other sites

Another example:

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

Example()

Func Example()
    Local $sGetData
    Local $sFilename = @ScriptDir & "\File.txt"
    Local $aFilename, $x = 10, $y = 10
        _FileReadToArray($sFilename, $aFilename, 0, ",")
            If @error Then Exit MsgBox(4096, "File Read Error", "Unable to read file into an Array")
            If UBound($aFilename, 2) <= 1 Then Exit MsgBox(4096, "File Read Error", "File cannot be stored in 2d Array")
    Local $aControl = $aFilename
    GUICreate("", 330, 10 + (30 * UBound($aFilename) - 1) + 40)
    $iStart = GUICtrlCreateDummy()
    For $i = 0 To UBound($aFilename) - 1
        For $j = 0 To UBound($aFilename, 2) - 1
            $aControl[$i][$j] = GUICtrlCreateInput($aFilename[$i][$j], $x, $y, 100, 20)
            $x += 105
        Next
        $x = 10
        $y += 30
    Next
    Local $idData = GUICtrlCreateButton("Get Data", 220, $y, 100, 30)
    GUISetState()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idData
                For $i = 0 To UBound($aControl) - 1
                    $sGetData = ""
                    For $j = 0 To UBound($aControl, 2) - 1
                        $sGetData &= GUICtrlRead($aControl[$i][$j]) & @CRLF
                    Next
                    MsgBox(4096, $i & " Line Data", $sGetData)
                Next
        EndSwitch
    WEnd
EndFunc

 

Share this post


Link to post
Share on other sites
21 minutes ago, Subz said:

Another example:

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

Example()

Func Example()
    Local $sGetData
    Local $sFilename = @ScriptDir & "\File.txt"
    Local $aFilename, $x = 10, $y = 10
        _FileReadToArray($sFilename, $aFilename, 0, ",")
            If @error Then Exit MsgBox(4096, "File Read Error", "Unable to read file into an Array")
            If UBound($aFilename, 2) <= 1 Then Exit MsgBox(4096, "File Read Error", "File cannot be stored in 2d Array")
    Local $aControl = $aFilename
    GUICreate("", 330, 10 + (30 * UBound($aFilename) - 1) + 40)
    $iStart = GUICtrlCreateDummy()
    For $i = 0 To UBound($aFilename) - 1
        For $j = 0 To UBound($aFilename, 2) - 1
            $aControl[$i][$j] = GUICtrlCreateInput($aFilename[$i][$j], $x, $y, 100, 20)
            $x += 105
        Next
        $x = 10
        $y += 30
    Next
    Local $idData = GUICtrlCreateButton("Get Data", 220, $y, 100, 30)
    GUISetState()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idData
                For $i = 0 To UBound($aControl) - 1
                    $sGetData = ""
                    For $j = 0 To UBound($aControl, 2) - 1
                        $sGetData &= GUICtrlRead($aControl[$i][$j]) & @CRLF
                    Next
                    MsgBox(4096, $i & " Line Data", $sGetData)
                Next
        EndSwitch
    WEnd
EndFunc

 

Thanks. Its really good working for me, but i dont know how to make print its all in Edit1:

Excample: After click GetData all data from input put to Edit1 in one line:

Edit1:

Name is: Michael XYZ, 21 years old, Boston city, Name is: Olex Marshal, 17 years old, Boston city,Name is: Alex XYZ, 19 years old, Boston city,

 

Share this post


Link to post
Share on other sites
#include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $sGetData
    Local $sFilename = @ScriptDir & "\File.txt"
    Local $aFilename, $x = 10, $y = 10
        _FileReadToArray($sFilename, $aFilename, 0, ",")
            If @error Then Exit MsgBox(4096, "File Read Error", "Unable to read file into an Array")
            If UBound($aFilename, 2) <= 1 Then Exit MsgBox(4096, "File Read Error", "File cannot be stored in 2d Array")
    Local $aControl = $aFilename
    GUICreate("", 330, 10 + (30 * UBound($aFilename) - 1) + 40)
    $iStart = GUICtrlCreateDummy()
    For $i = 0 To UBound($aFilename) - 1
        For $j = 0 To UBound($aFilename, 2) - 1
            $aControl[$i][$j] = GUICtrlCreateInput($aFilename[$i][$j], $x, $y, 100, 20)
            $x += 105
        Next
        $x = 10
        $y += 30
    Next
    Local $idData = GUICtrlCreateButton("Get Data", 220, $y, 100, 30)
    GUISetState()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idData
                For $i = 0 To UBound($aControl) - 1
                    MsgBox(4096, $i & " Line Data", 'Name is: '&GUICtrlRead($aControl[$i][0])&', '&GUICtrlRead($aControl[$i][1])&' years old, '&GUICtrlRead($aControl[$i][2])&' city')
                Next
        EndSwitch
    WEnd
EndFunc

Like that?


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

Share this post


Link to post
Share on other sites

Hello! Thanks for answer, but i want like that in Edit1:

-------------------------
Info:
Name is: Michael XYZ, 21 years old, Boston city, Name is: Olex Marshal, 17 years old, Boston city,Name is: Alex XYZ, 19 years old, Boston city,
-------------------------

Now i have only the last data from last input, also without "Info:". I want to load all.

In txt i have:
Michael XYZ, 21, Boston
Olex Marshal, 17, Boston
Alex XYZ, 19, Boston

Now after click Get Data in Edit1 i have:

------------------------------
Name is: Michael XYZ, 21 years old, Boston city
-------------------------------

Code:

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

Example()

Func Example()
    Local $sGetData
    Local $sFilename = @ScriptDir & "\test.txt"
    Local $aFilename, $x = 10, $y = 10
        _FileReadToArray($sFilename, $aFilename, 0, ",")
            If @error Then Exit MsgBox(4096, "File Read Error", "Unable to read file into an Array")
            If UBound($aFilename, 2) <= 1 Then Exit MsgBox(4096, "File Read Error", "File cannot be stored in 2d Array")
    Local $aControl = $aFilename
    GUICreate("", 330, 10 + (30 * UBound($aFilename) - 1) + 100)
    $iStart = GUICtrlCreateDummy()
    For $i = 0 To UBound($aFilename) - 1
        For $j = 0 To UBound($aFilename, 2) - 1
            $aControl[$i][$j] = GUICtrlCreateInput($aFilename[$i][$j], $x, $y, 100, 20)
            $x += 105
        Next
        $x = 10
        $y += 30
    Next
    Local $idData = GUICtrlCreateButton("Get Data", 220, $y, 100, 30)
    Local $ed = GUICtrlCreateEdit("", 20, $y+35, 300, 60)
    ;250 SZEROKOŚĆ
    GUISetState()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
           Case $GUI_EVENT_CLOSE
              ExitLoop
            Case $idData

                For $i = 0 To UBound($aControl) - 1

                    Global $Edit1 = GUICtrlSetData(-1,"Info:"&@CRLF)
                    Global $Edit1 = GUICtrlSetData(-1,'Name is: '&GUICtrlRead($aControl[$i][0])&', '&GUICtrlRead($aControl[$i][1])&' years old, '&GUICtrlRead($aControl[$i][2])&' city')

                Next
        EndSwitch
    WEnd
EndFunc

 

Please help. :)

Share this post


Link to post
Share on other sites

Do you mean something like:

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

Example()

Func Example()
    Local $sGetData, $sEdit = ""
    Local $sFilename = @ScriptDir & "\File.txt"
    Local $aFilename, $x = 10, $y = 10
        _FileReadToArray($sFilename, $aFilename, 0, ",")
            If @error Then Exit MsgBox(4096, "File Read Error", "Unable to read file into an Array")
            If UBound($aFilename, 2) <= 1 Then Exit MsgBox(4096, "File Read Error", "File cannot be stored in 2d Array")
    Local $aControl = $aFilename
    GUICreate("", 330, 10 + (30 * UBound($aFilename) - 1) + 100)
    $iStart = GUICtrlCreateDummy()
    For $i = 0 To UBound($aFilename) - 1
        For $j = 0 To UBound($aFilename, 2) - 1
            $aControl[$i][$j] = GUICtrlCreateInput($aFilename[$i][$j], $x, $y, 100, 20)
            $x += 105
        Next
        $x = 10
        $y += 30
    Next
    Local $idData = GUICtrlCreateButton("Get Data", 220, $y, 100, 30)
    Local $idEdit = GUICtrlCreateEdit("", 20, $y+35, 300, 60)
    ;250 SZEROKOŚĆ
    GUISetState()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
           Case $GUI_EVENT_CLOSE
              ExitLoop
            Case $idData
                GUICtrlSetData($idEdit, "")
                $sEdit = "Info:" & @CRLF
                For $i = 0 To UBound($aControl) - 1
                    $sEdit &= "Name is: " & GUICtrlRead($aControl[$i][0]) & ", " & GUICtrlRead($aControl[$i][1]) & " years old, " & GUICtrlRead($aControl[$i][2]) & " city" & @CRLF
                Next
                GUICtrlSetData($idEdit, $sEdit)
        EndSwitch
    WEnd
EndFunc

 

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

    • By Blueman
      Hey Guys,
      Maybe you can help me with something small.
      It is not terrible but it slows down my script and after a while even crash the script.

      First i explain what i have;
      I have a GUI with a lot of GUICtrlCreateInput controls, lets say 600 pieces.
      All the controls are hidden when the GUI is displayed, i have 3 buttons attached to 200 pieces each.
      When i press button-1 the first 200 controls are showed,
      When i press button-2 the first 200 controls are hidden and the next 200 controls are showed,
      etc, etc.
      This is working perfectly but after a while the GUI is getting slower and slower.
      After a while the controls aren't responding anymore and the script crashes.
      When i compile the script to 64-Bits version the speed is a lot better but you can still see the script slowing down each time i use the buttons.
      I think there has to be a buffer somewhere that is filling each time i show and hide the controls, but how can i clear that buffer without restarting the script?
      Deleting and re-creating the controls didn't help, deleting the entire gui (and controls) and re-creating it didn't help also.
      The only thing that is working is restarting the script.

      Thanks guys!

      PS: If you need a working example i can add it to this topic, just let me know. It is not rocket science, i just create the controls and show/hide them with a while loop and 3 buttons.
       
    • By XinYoung
      Hello again 
      I need to click this Submit button, but it's in a lightbox and has no ID (i think) and I can't figure out a way to target it. _IEFormSubmit doesn't work.

       
      Thank you
    • By oceanwind
      Hi All:
       How to set the input border color?
      tks in advance
    • By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • By Seminko
      Hey,
      i would like to set a value into an INPUT field.
      Checked the _IEFormElementSetValue function but that does require _IEFormGetObjByName and this is where the problem comes in. The input field I want to write to is not a part of a form tag. It is part of a table.
      <input type="text" class="w2" id="nabidka_vozidel_formular_tach_od" name="nabidka_vozidel_formular_tach_od" onchange="GLOBAL.pocetInzerceNZ(&quot;nabidka_vozidel_formular&quot;,&quot;tach_od&quot;,&quot;&quot;);" autocomplete="off"> I tried this but that didn't work:
      $oDownloadSamples = _IEGetObjById($oIE, "nabidka_vozidel_formular_tach_od") _IEFormElementSetValue($oDownloadSamples, "123") If you want to try the site I'm working with is https://www.tipcars.cz/. There is a menu on the top left hand side and if you click the "vyhledat" button the input fields will show up.
      Thanks
×
×
  • Create New...