Jump to content

find a particular line


Recommended Posts

I am looking for a way to read a file line by line until it finds a particular line then stop

While 1
Local $line = FileReadLine($file)
If @error = -1 Then ExitLoop
If $line == "Result Code: Succeeded" Then
MsgBox(0,"Line","hello")
Else
MsgBox(0,"Line",$line)
EndIf
WEnd

After a while this just stop displaying a message box.

Edited by mrjoli021
Link to comment
Share on other sites

Yes, it stops displaying the message box because it has reached the end of the file I'm guessing, that's what the ExitLoop will do.

By the way, unless you are intending to test the strings with case sensitivity, don't use the == because that is its function; string comparison with case sensitivity.

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

Link to comment
Share on other sites

  • Moderators

There are a number of ways to do it. Something like this, perhaps:

#include <File.au3>

$file = @DesktopDir & "Test.txt"
$lines = _FileCountLines($file)
 
  for $i = 0 to $lines
     $output = FileReadLine($file, $i)
         if StringInStr($output, "My line") Then
            MsgBox(0, "", "Found it at line " & $i)
         EndIf
  Next

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I would go the route of JLogan as well, using StringInStr() to search for what you're looking for in the string as opposed to attempting a direct match. Never know when invisible characters may spring up in a string that aren't accounted for when reading a file. :D

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

I would do it this way myself.

$file = @DesktopDir & "Test.txt"

$i = 1
$hFile = FileOpen($file)
While 1
     $line = FileReadLine($hFile)
     If @error = -1 Then ExitLoop
     If StringInStr($line, "My data") Then
         ConsoleWrite("Found it at line " & $i & @lf)
     EndIf
     $I += 1
WEnd

Using _FileCountLines and FileReadLine with a line number is extremely slow, on a test file I tried it on (with about 8400 lines), the FileCountLines and FileReadLine with line number took about 36 seconds on my computer, my method took less than 1/10th of a second on average.

Edited by BrewManNH

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

Link to comment
Share on other sites

for $i = 0 to $lines
     $output = FileReadLine($file, $i)
         if StringInStr($output, "Result Code: Succeeded") Then
   MsgBox(0, "", "Line found" & $i)
         Else 
   MsgBox(0,"", "Line not found")
   EndIf
  Next

I know that "Result Code: Succeeded" is in the file but It still says "line not found" I attempted to change it to something else and nothing.

Link to comment
Share on other sites

I'm not a fan of _FileCountLines() either for this use... omitting it also keeps you from having to parse the File.au3 UDF... which overall saves time and memory.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

Hi,

I am with BrewManNH on this. Never use a loop to pass a line number to FileReadLine unless you have a Step value significantly greater than 1. If you do use the line parameter then you must also read the Remarks section of the Help file:

From a performance standpoint it is a bad idea to read line by line specifying "line" parameter whose value is incrementing by one.
This forces AutoIt to reread the file from the beginning until it reach the specified line.

So you can see there is likely to be an enormous performance hit. Much better to let AutoIt do the counting for you. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Also, you don't have to run through the file twice once to count the lines then to read the lines.

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...