Sign in to follow this  
Followers 0
Agent Orange

Getting line of text from a txt file

10 posts in this topic

I need to grab a line of text from a text file copy that data into a variable...use that variable then loop around grab the very next line from that text file copy it into the variable and use it in the loop....doing this over and over again until it has gone through all the lines of text....any ideas?

Thanks

Share this post


Link to post
Share on other sites



I need to grab a line of text from a text file copy that data into a variable...use that variable then loop around grab the very next line from that text file copy it into the variable and use it in the loop....doing this over and over again until it has gone through all the lines of text....any ideas?

Thanks

Sorry for this little out of subject, but does my name make sense for u Agent ?

eX

Share this post


Link to post
Share on other sites

Sorry for this little out of subject, but does my name make sense for u Agent ?

eX

No

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Maybe this'll help...

#include <File.au3>

Dim $a_file, $x

Func search()
    _FileReadToArray(@ScriptDir & '\your.txt', $a_file)
    For $i = 1 To $a_file[0]
        If StringInStr($a_file[$i], 'your_word') Then
            MsgBox(0, 'Found', 'your_word found on line ' & $i)
            ExitLoop
        EndIf
    Next
    For $x = $i + 1 To $a_file[0]  ;edit: added + 1 so it starts at the next line
        MsgBox(0, 'Next Lines', $a_file[$x])
    Next
EndFunc
Edited by xcal

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Maybe this'll help...

#include <File.au3>

Dim $a_file, $x

Func search()
    _FileReadToArray(@ScriptDir & '\your.txt', $a_file)
    For $i = 1 To $a_file[0]
        If StringInStr($a_file[$i], 'your_word') Then
            MsgBox(0, 'Found', 'your_word found on line ' & $i)
            ExitLoop
        EndIf
    Next
    For $x = $i + 1 To $a_file[0]  ;edit: added + 1 so it starts at the next line
        MsgBox(0, 'Next Lines', $a_file[$x])
    Next
EndFunc
Xcal-

Thanks for the response. When I try this code modified to work in my situation I don't get any values returned when I test that $a_file variable. There is nothing in it.

Any ideas

Edited by Agent Orange

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

$a_file will actually be an array of the file separated line by line.

$a_file[0] is total lines

$a_file[1] is line 1

$a_file[2] is line 2

$a_file[3] is line 3

and so on...

The first loop in the function is exited when it finds the line with your search word, so, when that first loop is exited, $i is the first line to start whatever you want to do. $i + 1 is where to start working for the next loop, which is the next line. $x is the iteration of the second loop. So, $a_file[$x] is all the following lines while in that loop.

I hope that somehow makes sense. I'm taking painkillers for a bad tooth right now haha...

edit

I think this looks better, actually...

#include <File.au3>

Dim $a_file, $x

Func search()
    _FileReadToArray(@ScriptDir & '\your.txt', $a_file)
    For $i = 1 To $a_file[0]
        If StringInStr($a_file[$i], 'your_word') Then ExitLoop
    Next
    For $x = $i To $a_file[0]
        MsgBox(0, 'The Lines', $a_file[$x])  ;*note
    Next
EndFunc

* do your stuff here. $a_file[$x] starts at the first line found by your_word (some search word indicating this is the line you want to start at), and loops to the end of the array (last line). Make changes in the array, then write it back to the file, if you want.

Edited by xcal

Share this post


Link to post
Share on other sites

Xcal-

Thanks again for your help. I still can't seem to get it to work. If I put a number...for example MsgBox(0, 'Next Lines', $a_file[$1])....it returns the proper text line. If I don't it returns a number 477

Here is what I have

#include <File.au3>

Dim $a_file, $x, $sFilePath, $i

_FileReadToArray('f:\selist.txt', $a_file)

For $i = 1 To $a_file[0]

If StringInStr($a_file[$i], '') Then ExitLoop

Next

Run("C:\Program Files\Knowlix\IKnowAuthor.exe","",@SW_MAXIMIZE)

MsgBox(0,"test",$a_file[$x])

sleep(40000)

MsgBox(0,"test","",$a_file[$x])

WinWaitActive("KnowlixAuthor")

ControlSend("KnowlixAuthor","","TEdit1",$a_file[$x])

Sleep(400)

ControlClick("KnowlixAuthor","","TButton1")

sleep(1500)

Send("{TAB}")

Send("!f")

Send("p")

sleep(900)

Sleep(1500)

ControlClick("Print","","TButton2")

WinWaitActive("Save PDF File As")

FileChangeDir("F:\iknow_pdfs\SE\")

Send("f:\iknow_pdfs\SE\" & $a_file[$x])

Sleep(200)

ControlClick("Save PDF File As","","Button2")

WinWaitActive("Adobe Acrobat Professional")

Send("^q") ;closes acrobat

Send("!f")

Send("x")

sleep(300)

WinWaitActive("KnowlixAuthor")

Send("!f")

Send("x")

Send("!f")

Send("x")

For $x = $i To $a_file[0]

MsgBox(0, 'Next Lines', $a_file[$x])

Next

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Try this... note the problem:

#include <File.au3>

Dim $a_file, $sFilePath, $i

_FileReadToArray('f:\selist.txt', $a_file)
For $i = 1 To $a_file[0]
    If StringInStr($a_file[$i], '') Then ExitLoop  ;<== * problem
Next

Run("C:\Program Files\Knowlix\IKnowAuthor.exe", "", @SW_MAXIMIZE)
MsgBox(0, "test", $a_file[$i])
Sleep(40000)
MsgBox(0, "test", "", $a_file[$i + 1])
WinWaitActive("KnowlixAuthor")
ControlSend("KnowlixAuthor", "", "TEdit1", $a_file[$i + 2])
Sleep(400)
ControlClick("KnowlixAuthor", "", "TButton1")
Sleep(1500)
Send("{TAB}")
Send("!f")
Send("p")
Sleep(900)
Sleep(1500)
ControlClick("Print", "", "TButton2")
WinWaitActive("Save PDF File As")
FileChangeDir("F:\iknow_pdfs\SE\")
Send("f:\iknow_pdfs\SE\" & $a_file[$i + 3])
Sleep(200)
ControlClick("Save PDF File As", "", "Button2")
WinWaitActive("Adobe Acrobat Professional")
Send("^q") ;closes acrobat
Send("!f")
Send("x")
Sleep(300)
WinWaitActive("KnowlixAuthor")
Send("!f")
Send("x")
Send("!f")
Send("x")


;That line needs a way to know where to start from.  '' is nothing (anything in this case), so it'll exit the loop immediately.  Put in some key text: 'start line always contains this text'

edit - added in the + 1, + 2, + 3... I'm assuming you want those to be the lines following your first one. Removed the second ($x) loop, since it looks like you don't need that. :)

another edit - btw, I've been assuming that the first line you want to start from isn't on line 1. Is this correct?

Edited by xcal

Share this post


Link to post
Share on other sites

Xcal-

That worked! Thanks

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  
Followers 0