Jump to content
Sign in to follow this  
sosimple

Search for a word in a txt file?

Recommended Posts

sosimple

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,

Share this post


Link to post
Share on other sites
searchresult

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

Share this post


Link to post
Share on other sites
BrettF

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

Share this post


Link to post
Share on other sites
ResNullius

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.

Share this post


Link to post
Share on other sites
sosimple

Thanks alot!! This is ok for what i am doing!

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  

×