Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TugboatEng on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a string from text file using autoit

Status
Not open for further replies.

rk_19

Structural
Joined
Aug 7, 2012
Messages
71
Location
AE
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


thanks
 
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

while(True)
if (@error = -1) then exitloop


do stuff

$inputline = filereadline($workfile)
wend


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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top