Jump to content
Lisuter

How to read from txt file

Recommended Posts

Lisuter

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
Lisuter

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
JLogan3o13

@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.

  • Thanks 1

√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
Subz

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 1

Share this post


Link to post
Share on other sites
Lisuter
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
careca
#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?

  • Thanks 1

Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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
Lisuter

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
Subz

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

    • oceanwind
      By oceanwind
      Hi All:
       How to set the input border color?
      tks in advance
    • Shirdish_chakravarthi
      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

    • Seminko
      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
    • way1000
      By way1000
      i'm trying to create a program that let's the user type a word in an input field (gui) and when a button is pressed the letters of the word are replaced by 1 word each from a list of words in a text file
      i created the gui for it but i need help to add some more code to make it work like intented

      each letter of a word should be replaced by a word from a text file
      the text file is a source for replacing letters is a file with 2 columns separated by tab that contains the entire alphabet (26 rows)
       
      please help if possible
       
      i posted an example gui screenshot

    • nacerbaaziz
      By nacerbaaziz
      hello
      Greetings to all
      dears i have a big problem in the bass.dll
      e.g
      If you play an audio file using the bass.dll
      and  You changed the audio output
      Such as setting or removing headphones
      The sound output of bass.dll does not change
      Is there any solution
      If it exists, please give it to me
      Greetings and thanks in advance
      I apologize for my bad English
×