Jump to content
Sign in to follow this  
USMCGalloway

File Search

Recommended Posts

USMCGalloway

I have looked around, and have found a lot of examples for looking through a file and looking for specific strings. I have not seen anything for getting the next line after a specific string is found. 

Example: 

Yada 

Yada

Yada

Yada

STRING

What I want, but do not know what the string is going to be

Yada

Yada

#include <File.au3>
#include<Array.au3>


$file = "C:\Users\Me\Documents\Test.txt"
FileOpen($file, 0)

For $i = 1 to _FileCountLines($file)
      $line = FileReadLine($file, $i)
     $knownString = "Current"   
      If $knownString  == StringLeft($line, 7) Then
         $e=0
         $c = $i + 1
         $array[$e] = FileReadLine($file, $c)
         $e = $e + 1
      EndIf      
Next
FileClose($file)

_ArrayDisplay($array)

The Error I am getting is:

(14) : ==> Expected a "=" operator in assignment statement.:

$array[$e] = FileReadLine($file, $c)
$array^ ERROR
 
Am I trying to use something as it is not suppose to be used?
 
As always any help is greatly appreciated. 

Share this post


Link to post
Share on other sites
BrewManNH

Try it this way and eliminate a lot of things in your script that aren't necessary.

#include<Array.au3>


$file = "C:\Users\Me\Documents\Test.txt"
$hFile = FileOpen($hFile)
Local $knownString = "Current", $e = 0, $array[100], $line
While 1
    $line = FileReadLine($hFile)
    If @error = -1 Then ExitLoop
    If $knownString = StringLeft($line, 7) Then
        If UBound($array) >= $e Then ReDim $array[UBound($array) + 100]
        $array[$e] = FileReadLine($hFile)
        $e = $e + 1
    EndIf
WEnd
FileClose($hFile)
Redim $array($e + 1)
_ArrayDisplay($array)
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

Share this post


Link to post
Share on other sites
USMCGalloway

Thank you that is much cleaner that what I had worked up. 

Share this post


Link to post
Share on other sites
BrewManNH

I updated the script above to eliminate the empty elements in the $array array after reading through the file. It's just a ReDim line above the _ArrayDisplay line.


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

Share this post


Link to post
Share on other sites
kylomas

Because I read jchd's excellent rewrite of SRE Help file doc and THINK I understand them again...

local $filestr = 'Yada1' & @crlf & 'yada2' & @lf & 'yada3' & @cr & 'string' & @cr & 'The string that I want' & @crlf & 'yada4' & 'yada5'

ConsoleWrite(_srch('yada3') & @LF)
ConsoleWrite(_srch('what') & @LF)
ConsoleWrite(_srch('string') & @LF)

func _srch($str)

    local $aTmp = stringregexp($filestr,$str & '.*?\R(.*?)\R',3)
    if @error = 0 then
        return $aTmp[0]
    Else
        return 'Not found'
    EndIf

endfunc

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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  

×