Jump to content
spoolin1

Search textfile for string then return the next line

Recommended Posts

$file = FileOpen("file.txt", 0)
$read = FileRead($file)
If @error = -1 Then
    MsgBox(0, "Error", "File not read")
    Exit
Else
    ;;;MsgBox(0, "Read", $read)
    If StringRegExp($read, "______Part Status______") Then
        MsgBox(0, "Oops", "Match")
    Else
        MsgBox(0, "Oops", "No match")
    EndIf
EndIf
FileClose($file)

 

I found this code and works great finding a string.  What I would like to do is have it look at the next line after a string and assign it to a variable then go from there.  End goal is to check if there is any text on the next line after string or if its empty.   

 

Edited by spoolin1

Share this post


Link to post
Share on other sites

spoolin1,

Or read the file into an array (_FileReadToArray) and then search the array - when you find the match you just need to read the next element.

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

 

Share this post


Link to post
Share on other sites

An example using StringRegExpReplace():-

; ---- Create a test text file ------
Local $sText = "", $sOutFile = "output.txt"
For $i = 1 To 6000
    $sText &= (Mod($i, 100) ? "Line " & StringRight("0000" & $i, 4) : "") & @CRLF ; Every 100th line is blank.
Next
FileWrite($sOutFile, $sText)
;ConsoleWrite($sText & @CRLF)

; -------- Find the next line ----------
Local $sSearch = "Line 2999" ; The next line will be blank.
;Local $sSearch = "line 2995" ; The next line will be non-blank. (Remove leading comment character at start of this line to test)
Local $sNextLine = StringRegExpReplace(FileRead($sOutFile), "(?is)^.*" & $sSearch & "\R([^\v]*).*$", "$1") ; Case insensitive "(?i)"

MsgBox(0, "Next line", 'The next line after the line, "' & $sSearch & ' is ' & ($sNextLine = "" ? "blank." : @CRLF & '"' & $sNextLine & '".'))

If FileExists($sOutFile) Then FileDelete($sOutFile) ; Clean up the created test text file.

 

Share this post


Link to post
Share on other sites

Be careful with regex which needs precise requirements : is the string a part of the line or the whole line, can several occurences of the string exist in the file, is a line considered as 'empty' even when containing only white spaces, etc
(!)

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

×
×
  • Create New...