Sign in to follow this  
Followers 0
Guillermo

Read lines in a text file until last one

5 posts in this topic

Hi,

I want my script to read all lines in a text file and then stop when it reaches the end. I've figured out how to loop through all lines, but my script will never exit that loop.

I swear I've read the "@error" and the "return" help topics and it's sad but I don't understand a thing.

Can anyone please help? I've run out of ideas.

$file=FileOpen("myfile.txt",0)


$nr=1
While @error > 0
    
$result=FileReadLine($file,$nr)
$nr=$nr+1

WEnd

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$file=FileOpen("myfile.txt",0)

While 1
  $result=FileReadLine($file)
  If @error = -1 Then ExitLoop; end of file

; do something with $result ...
WEnd

FileClose($file)

There is no need to use parameter Line ($nr) in FileReadLine()

EDIT: this is also in example at function FileReadLine() in help file

Edited by Zedna

Share this post


Link to post
Share on other sites

$file=FileOpen("myfile.txt",0)

While 1
  $result=FileReadLine($file)
  If @error = -1 Then ExitLoop; end of file

; do something with $result ...
WEnd

FileClose($file)

There is no need to use parameter Line ($nr) in FileReadLine()

:P

It can't be that easy!

:mad2:

(That means: thanks.) ;)

Share this post


Link to post
Share on other sites

:P

It can't be that easy!

:mad2:

(That means: thanks.) ;)

Its the simple syntax like this, that produce results beyond our

expectation, that makes Autoit the best of its breed.

Keep at it, its worth the effort.

HardCopy


Contributions: UDF _DateYearFirstChildren are like Farts, you can just about stand your own.Why am I not a Vegetarian?...Well...my ancestors didn't fight & evolve to the Top of the food chain for me to survive on Salad

Share this post


Link to post
Share on other sites

this is also in example at function FileReadLine() in help file

I can't believe I didn't see it in the first place. :P

Well, here's the final script I've come up with. You can't imagine how much time it saves me at work! ;) Basically it looks up terms in a glossary file, retrieves a translation and then replaces the initial searched term with it (in a Word or Excel file, for instance). If no match is found, then the user can add one and it'll be saved to the glossary.

As it is the first useful piece of code I've written, I'd be glad to receive critics from more experienced script writers. Am I using variables correctly, etc?

I'm already planning to update it with more features, so I want to build it up from a reliable base.

opt("TrayIconDebug", 1); ==> Shows debug info through tray icon.
opt("MustDeclareVars", 1); ==> Prevents typos in variable names.



Dim $file, $result, $searchterm, $spa, $eng, $termglos

$file = FileOpen("glosario.txt", 0); ==> Internal id of the opened file "glossario.txt".

Send("^c")
$searchterm = ClipGet();==> Term to look up, taken from the Clipboard. User MUST select a term in a file before running the script with a keyboard shortcut.

While 1; ==> The term is looked up in the file "glossario.txt"...
    $result = FileReadLine($file)
    
    If $result = "" Then ExitLoop;==> This is needed because if there's a blank line in the glossary or in a pair of terms the script would throw an error otherwise. The script shoud delete invalid entries as it goes along.
    If @error = -1 Then ExitLoop;==> The term can't be found in the glossary...
    
    $termglos = StringSplit($result, ",");==> The delimiter for the term and its translation in the glossary is a comma, like "Term,Término".
    $eng = $termglos[1]
    $spa = $termglos[2]
    
    If $searchterm = $eng Then;==> The term is found in the glossary... (Not case sensitive, better so.)
        ClipPut($spa)
        Send("^n" & "^n" & "^n" & "^n" & "^n" & "^n");==> Makes the to-be-replaced text briefly blink so the user realizes the change (Word & Excel). Varies depending on locale version ("n" for Spanish, "b" for "English")!!
        FileClose($file)
        Send("^v");==> Term replaced!
        Exit
    EndIf
    
WEnd


Dim $newterm;==> If the text isn't found...

FileClose($file)
MsgBox(64, "Term not found", "The term couldn't be found in the glossary.")
$newterm = InputBox("New translation", "Enter a translation for the searched term or leave the field blank to cancel." _
         & @CRLF & @CRLF & $searchterm, "Término");==> Input for new term. If the searched term is misspelled, this will fill the glossary with clutter.

Select;==> Checks $newterm for errors. The script should also check if the translation's already been used for a different term.
    Case $newterm = ""
        Exit
    Case StringInStr($newterm, ",") > 0
        MsgBox(48, "Invalid entry", "Your translation cannot contain commas." & @CRLF & "Please look the term up again to add a translation.")
        Exit
    Case StringLen($newterm) < 2
        MsgBox(48, "Invalid entry", "Your translation must be at least two characters long." & @CRLF & "Please look the term up again to add a translation.")
        Exit
    Case Else
        $file = FileOpen("glosario.txt", 1)
        FileWriteLine($file, $searchterm & "," & $newterm & @CRLF)
        FileClose($file)
EndSelect

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