Jump to content

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")
    ;;;MsgBox(0, "Read", $read)
    If StringRegExp($read, "______Part Status______") Then
        MsgBox(0, "Oops", "Match")
        MsgBox(0, "Oops", "No match")


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

Instead of using StringRegExp use StringInStr that will give you the position of the string and then calculate how long is the searched string and see what's after that with StringMid.

Link to comment
Share on other sites

  • Moderators


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.


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:


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

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.
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.


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

  • Recently Browsing   0 members

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