Jump to content

Reading TXT File Line by Line and putting in a TXTBOX


Recommended Posts

Hi,

I'm new here and just started playing with AutoIt..

By reading some tut, I was able to create a simple script to open a particular website.

Now my problem is how to:

1. Open a save txt file with my data in it..like this:

Name

Family Name

Email Address

and put it in a textbox/data field of IE?

Also how to put may text on datafield by using a known MARKER like

blahbla@MYTEXT_HERE

@ is the marker..

TIA

Link to comment
Share on other sites

You would need these functions:

FileOpen()

FileReadtoArray() (or something similar)

Using some string expressions, determine what exp. is for name, etc. and place these values into the right fields

Use another string expression for @

Link to comment
Share on other sites

I wouldn't read files line by line (except the very large ones) but use - as has been suggested - _FileReadToArray for performance reasons.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

For the IE part I suggest to use the IE UDF that comes with AutoIt.

What have you coded so far?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

For the IE part I suggest to use the IE UDF that comes with AutoIt.

What have you coded so far?

This is my code to find the input field and fill in the data and press login button..

#include <IE.au3>

Call("domain")

Func domain()

Global $oIE = _IECreate("http://www.mypage.org",0,1,1) ; page to open

Local $oUsername = _IEGetObjByName($oIE, "Username")
Local $oPassword = _IEGetObjByName($oIE, "Password")
Local $oButton = _IEGetObjById($oIE, "btnLogin")

_IEFormElementSetValue($oUsername, "user")
_IEFormElementSetValue($oPassword, "user")
_IEAction($oButton, "click")

_IEQuit($oIE) ;close the Internet Explorer

EndFunc ;==>login

I want to remove this part:

_IEFormElementSetValue($oUsername, "user")
_IEFormElementSetValue($oPassword, "user")

and read it directly from text file named, data.txt

BR

Edited by Hodahel
Link to comment
Share on other sites

Use FileReadLine to read a line to a variable and then pass this variable to _IEFormElementSetValue.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Rules violation. Auto-log-in is not allowed. See rules link in bottom right corner of page.

#include <IE.au3>

Call("domain")

Func domain()

Global $oIE = _IECreate("http://www.mypage.org",0,1,1) ; page to open

Local $oUsername = _IEGetObjByName($oIE, "Username")
Local $oPassword = _IEGetObjByName($oIE, "Password")
Local $oButton = _IEGetObjById($oIE, "btnLogin")

_IEFormElementSetValue($oUsername, "user")
_IEFormElementSetValue($oPassword, "user")
_IEAction($oButton, "click")

_IEQuit($oIE) ;close the Internet Explorer

EndFunc ;==>login

edit: pointed to rules link Edited by JohnQSmith

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

  • Moderators

JohnQSmith,

The rules speak of "log-in and security dialogs" - as this deals with a normal page there is no problem. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Rules violation. Auto-log-in is not allowed. See rules link in bottom right corner of page.

edit: pointed to rules link

With due respect sir..

Bypassing of security measures - log-in and security dialogs.....

I don't see any "BYPASS of security" if I login with all my credentials? Not unless if you consider NOT TYPING the login name as Bypassing of security..

Link to comment
Share on other sites

  • Moderators

Hodahel,

See my post above - there is no problem with this question. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

I already tried this code on AutoIt Example..

Local $file = FileOpen("test.txt", 0)
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
Local $line = FileReadLine($file)
If @error = -1 Then ExitLoop
MsgBox(0,"Line read:", $line)
WEnd

FileClose($file)

And I created a form in Koda

post-78616-0-02452500-1363790882_thumb.p

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\users\user\documents\hunt.kxf
$Form1_1 = GUICreate("Form1", 300, 241, 407, 256)
$Input1 = GUICtrlCreateInput("", 72, 16, 209, 21)
$Label1 = GUICtrlCreateLabel("TextBox1", 24, 16, 49, 17)
$Button1 = GUICtrlCreateButton("Start", 88, 112, 113, 41)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd

All I want is when I click the start button, the first line of the text file will be displayed on the textbox of the form..Can somebody help me to do it?

TIA

Link to comment
Share on other sites

  • Moderators

Hodahel,

I would read the file like this:

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

; Read the file into an array
Global $aLines
_FileReadToArray("Test.txt", $aLines)

; Create the GUI
$Form1_1 = GUICreate("Form1", 300, 241, 407, 256)
$Input1 = GUICtrlCreateInput("", 72, 16, 209, 21)
$Label1 = GUICtrlCreateLabel("TextBox1", 24, 16, 49, 17)
$Button1 = GUICtrlCreateButton("Start", 88, 112, 113, 41)
GUISetState(@SW_SHOW)

; This is the line number we are going to read
$iCount = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ; Increase the line number by 1
            $iCount += 1
            ; Check we are not past the end of the file
            If $iCount > $aLines[0] Then
                ; We were
                GUICtrlSetData($Input1, "End Of File")
            Else
                ; Write the next line into the input
                GUICtrlSetData($Input1, $aLines[$iCount])
            EndIf
    EndSwitch
WEnd

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
GUICreate("Form1", 300, 241, 407, 256)
Global $Input1 = GUICtrlCreateInput("", 72, 16, 209, 21)
GUICtrlCreateLabel("TextBox1", 24, 16, 49, 17)
Global $Button1 = GUICtrlCreateButton("Start", 88, 112, 113, 41)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Local $file = FileOpen("test.txt")
            If $file = -1 Then
                MsgBox(0, "Error", "Unable to open file.")
                Exit
            EndIf
            GUICtrlSetData($Input1, FileReadLine($file)) ; reads only the first line of the file
            FileClose($file)
    EndSwitch
WEnd

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

  • 2 weeks later...

This is my progress now..

#include <IE.au3>
#Include <Constants.au3>
#include <File.au3>


Local $file = FileOpen("address.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

; Read in lines of text until the EOF is reached
;While 1
Local $line = FileReadLine($file)
If @error = -1 Then Exit
;MsgBox(0, "Line read:", $line)
;WEnd

Call("address")

Func address()

Global $oIE =_IECreate("http://"& $line &",0,1,1,1) ; page to open

_IEQuit($oIE) ;close the Internet Explorer

EndFunc ;==>address

FileClose($file)

I have a texe file named ADDRESS.TXT which list website address like this:

www.yahoo.com

www.google.com

www.msn.com

I want it to put it in IE address bar like this

Global $oIE =_IECreate("http://"& $line &",0,1,1,1)

until all the line is finished..

With that code above it only insert the first line of the text file...

Edited by Hodahel
Link to comment
Share on other sites

Hodahel,

See comments in code...

#include <IE.au3>
#include <Constants.au3>
#include <array.au3>
#include <file.au3>
Local $file = FileOpen(@scriptdir & "\address.txt", 0) ; <---------  file is in same dir as script
; Check if file opened for reading OK
If $file = -1 Then
 MsgBox(0, "Error", "Unable to open file.")
 Exit
EndIf
; Read file to array
local $aLines
_FileReadToArray(@scriptdir & "\address.txt", $aLines)
;Call("address")  ; <---------------------  invoke functions directly, not with call
address()   ; <---------------------  like this
Func address()
 for $1 = 1 to $aLines[0]
  if $aLines[$1] = '' then continueloop
  local  $oIE = _IECreate($aLines[$1],0,1,1,1) ; page to open
  if @error <> 0 then ConsoleWrite('Error code = ' & @error & @LF)
  ;_IEQuit($oIE) ;close the Internet Explorer
 next
EndFunc   ;==>address
FileClose($file)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Maybe I'm too old for this... :(

Too old to AutoIt, too young to die ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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