Jump to content

Search for a word in a txt file?


sosimple
 Share

Recommended Posts

Hi, can anyone help on this because i dont have much expiriance on files..

I have a file myfile.txt with words. I need a script serching for a word in there . For example serching for the word animal .

I only need to tell me if there is the word there, nothing else.

Thanks,

Link to comment
Share on other sites

Hi, can anyone help on this because i dont have much expiriance on files..

I have a file myfile.txt with words. I need a script serching for a word in there . For example serching for the word animal .

I only need to tell me if there is the word there, nothing else.

Thanks,

here is the code

#include <array.au3>

$FileLoad = Fileread("file.txt")

$WordLoad = StringSplit($FileLoad, " ")

$Input = InputBox("Word Search", "Please insert word to search")

$SearchResult = _ArraySearch($WordLoad, $Input) 

If $SearchResult = True Then
    MsgBox(0,"Search Rersult", "Word exists")
    
Else
    
    MsgBox(48,"Error", "Word doesn't exist")
    
EndIf
Edited by searchresult
Link to comment
Share on other sites

Another method:

$file = FileOpenDialog ("Open Wordlist", "", "TXT DOCUMENTS (*.txt)")
$search = InputBox ("Search Term", "Enter a term to search the file for", "", " M")
$hFile = FileOpen ($file, 0)
$text = FileRead  ($hFile)
FileClose ($hFile)

If StringInStr ($text, $search) <> @error Then
    MsgBox (0, "Success!", "String was found")
Else
    MsgBox (0, "Error!", "String was not found")
EndIf
Link to comment
Share on other sites

here is the code

#include <array.au3>

$FileLoad = Fileread("file.txt")

$WordLoad = StringSplit($FileLoad, " ")

$Input = InputBox("Word Search", "Please insert word to search")

$SearchResult = _ArraySearch($WordLoad, $Input) 

If $SearchResult = True Then
    MsgBox(0,"Search Rersult", "Word exists")
    
Else
    
    MsgBox(48,"Error", "Word doesn't exist")
    
EndIf
This code would need a modification to work properly.

The function for _ArraySearch states

Return Value(s): On Success - Returns the position of an item in an array.

On Failure - Returns an -1 if $vWhat2Find is not found

So in your example, not found evaluates to -1 which equals true which means found... Not!

The If line should be

If $SearchResult > 0 Then

Having said that, I favour Bert's example; should be faster.

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