Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


read a string from text file using autoit

read a string from text file using autoit

read a string from text file using autoit

Hi, i want to read a particular string from a text file using autoit. i wish to read it without explicitly opening the text file. the copied string should be then transferred to a excelbook (again, no need to open the excel book)...

with reference to my screenshot attached, my input to script is going to be 0017-0008, and the script should copy the highlighted two lines from the input file to excel



RE: read a string from text file using autoit

AutoIt can't create an Excel file directly - it, or you, will have to open the workbook.

AutoIt also can't read a text file without opening it. It doesn't require that you have it open in an editor.

$workfile_name = FileOpenDialog("Find a file to read","C:\","All (*.*)|Text files (*.txt)")
$workfile = fileopen($workfile_name, 0)

These two lines allow you to browse to the test file.

$look_for = InputBox ( "title", "prompt", "default")

This line asks the user what text is being searched for.
You can see the meanings of the fields in the AutoIt help file.

$inputline = filereadline($workfile,1)

This line reads the first line from the file that was chosen

if (@error = -1) then exitloop

do stuff

$inputline = filereadline($workfile)

These lines loop through all the lines in the text file.

The do stuff portion is the tricky part.

You can use the StringInStr() function to match the $look_for against the $inputline. Use an If statement, and if there is a match, and if the next line is always skipped, read a line into a second variable and then read the next line into the same variable.

Do an exitloop at the end of the If statement to stop reading once you have found what you are looking for.

After the loop you can run Excel; there's an example of how to run programs in the Tutorial section of the Help files under Simple Notepad Automation. It's more effort that I care to undertake to create the part of the program that interacts with you about whether to create a new workbook, open an existing one, and where in the workbook you want the data.

You can use Send() function to send a Ctrl-g to Excel to then set the destination cell in the active worksheet. SendI() is described in the Function Reference section of AutoIt help under the Keyboard management subsection. Note that it will attempt to send to the Active window; there is a function SendKeepActive() that will try to force an identified window to be active while Send() is working. Look for the section "Window Titles and Text (Advanced)" to determine how to tell AutoIt which window you want to send to. This is in the "Using AutoIt" section.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


White Paper: Industrial Control Basics: Contactors
A contactor is an electrical device used for switching an electrical circuit on or off. Considered to be a special type of relay, contactors are used in applications with higher current carrying capacity, while relays are used for lower current applications. Download Now
Research Report: State of IoT Adoption in Product Development 2019
This research report, based on a survey of 234 product development professionals, examines the current state of Internet of Things (IoT) adoption by product design teams, its perceived importance, and what features and capabilities teams consider important when making decision about adding IoT functionality to their products. Download Now
Research Report: Augmented Reality for Maintenance, Repair and Overhaul (MRO)
The term Industry 4.0 denotes a cluster of technologies that’s poised to fundamentally reshape manufacturing and bring about a new industrial revolution. These include 3D printing (AM), the Industrial Internet of Things (IIoT), artificial intelligence (AI) and mixed reality technologies, more commonly known as virtual reality (VR) and augmented reality (AR). Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close