Jump to content
Sign in to follow this  
Hodahel

Reading TXT File Line by Line and putting in a TXTBOX

Recommended Posts

Hodahel

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

Share this post


Link to post
Share on other sites
Relive

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 @

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Hodahel

I want to read 1 line, then put in an input field of IE., then click OK button..

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Hodahel

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

Share this post


Link to post
Share on other sites
water

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
JohnQSmith

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

Share this post


Link to post
Share on other sites
Melba23

JohnQSmith,

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Hodahel

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

Share this post


Link to post
Share on other sites
Melba23

Hodahel,

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

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Hodahel

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

I'll try to work with this..:D

BR

Share this post


Link to post
Share on other sites
Hodahel

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

Share this post


Link to post
Share on other sites
Melba23

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

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
BrewManNH

#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

  • Like 1

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

Share this post


Link to post
Share on other sites
Hodahel

Melba23,

BrewManNH

Thank you so much to both of you! I'll study both examples..

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

Edited by Hodahel

Share this post


Link to post
Share on other sites
Hodahel

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

Share this post


Link to post
Share on other sites
kylomas

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

  • Like 1

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

Share this post


Link to post
Share on other sites
water

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

Too old to AutoIt, too young to die ;)
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
kylomas

Too old to AutoIt, too young to die ;)

So much code...so little time...


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

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
Sign in to follow this  

×