Jump to content
Sign in to follow this  
Chobyhy

Searching Lines Within Txt File

Recommended Posts

Chobyhy

I'm trying to make a function where someone searches at least 1 word in the line, it will display the whole line.

Example:

Line1: Test asfa1515as fasdf125

Line2: asdf12 45478df1 1111

Is there a way if someone inputs "Test" into an inputbox for a search, that whole Line 1 gets displayed?

I dont think StringInStr would work since that only returns the position.

Share this post


Link to post
Share on other sites
fu2m8

I'm trying to make a function where someone searches at least 1 word in the line, it will display the whole line.

Example:

Line1: Test asfa1515as fasdf125

Line2: asdf12 45478df1 1111

Is there a way if someone inputs "Test" into an inputbox for a search, that whole Line 1 gets displayed?

I dont think StringInStr would work since that only returns the position.

You could probably use something like _FileReadToArray with a loop to do what your after.

#include <File.au3>

Dim $lines

$search = InputBox("Enter Search String", "Enter the string your looking for...")

_FileReadToArray(@ScriptDir & "\testFile.txt", $lines) ;<-- needs a file called testFile.txt in the same directory as the script

For $x = 1 To $lines[0] ;loops through each line in the text file
    If StringInStr($lines[$x], $search) Then ;reads each line of the file for the search
        MsgBox(64, "Match Found", "Found a match on line " & $x & ". The line is: " & @CRLF & $lines[$x]) ;prompt showing line number and full line
    EndIf
Next

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.