Sign in to follow this  
Followers 0
JohnBailey

search a file for a string

5 posts in this topic

There may be a much better way (faster, less script, and/or more options). If so, I'd like to read your critique!

;===============================================================================
; Description:      Search a file for a string and returns a string from the file or a binary.
; Parameter(s):     $stringtoSearchfor - string, Substring to search for
;                   $filetoSearch - string, The file to search
;                   $verify - binary, used if a single response/verification is desired in the return (0 = do not use, 1 = use) (default = 0)
;                   $arrayPostoReturn - number, The corresponding string to return (default is array 1 of the row where the string is found)
;                   $delimiters - string, The deliminter(s) to use in setting the array
; Return Value(s):  On Success - Returns a string from an array of the row where the string is found or just binary if $verify is used.
;                   On Failure - Returns 0
; Note(s):          If verified is used the delimiters and arrayPostoReturn is ignored
;===============================================================================
Func searchFileforString($stringtoSearchfor,$filetoSearch, $verify = 0, $arrayPostoReturn = 1, $delimiters = ',')
    For $r = 1 to _FileCountLines($filetoSearch)
        Local $SFSreadLine = FileReadLine($filetoSearch,$r)
        Local $SFSreadLineArray = StringSplit($SFSreadLine,$delimiters,1)
        Local $check = StringInStr($SFSreadLine,$stringtoSearchfor)
        Local $SFSresult
        If $verify = 0 Then
            If  $check > 0 Then
                $SFSresult = $SFSreadLineArray[$arrayPostoReturn]
                ExitLoop
            Else
                $SFSresult = 0 ; keeps searching file or ends
            EndIf
        Else
            If  $check > 0 Then
                $SFSresult = 1
                ExitLoop
            Else
                $SFSresult = 0 ; keeps searching file or ends
            EndIf
        EndIf
    Next
    Return $SFSresult
EndFunc

A decision is a powerful thing

Share this post


Link to post
Share on other sites



It looks good but it might have been easier to use

_FileReadToArray()

_ArraySearch()


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I think I could use a function like this. Another way to do it is to use the dos commands findstr or find. Or if you have control over the format of the file, use the IniRead() functions in Autoit. I was also looking on msdn for a function in one of the dlls that might do this but I had no luck finding one. I'm really not sure the best way to do this, I'm sure it depends on application of the function. Good luck!

Share this post


Link to post
Share on other sites

guys! Thanks for your ideas! Both of them are good. I searched on MSDN too, but my results were the same - null.

This seems like a simple function that would have had some class developed for it, especially a dll of sorts. I don't know.

GEOSoft, great idea with the array search! That also might be a great way of doing it. I should do some time testing and find out which is faster and which is easier to use. Again, thanks I didn't consider using those two.

I was hoping to make this flexible for what its able to return. I use this in several different scripts I have, and I'd like to make it more friendly for anyone else out there that might be able to use it. It would be wonderful to get it going well.

Any other opinions or critiques on anything in the script, please post! :shocked:

Should I use _ArraySearch over my current method, and why?

Thanks gang, and if I come across something better I will post it here.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

I'm going to rewrite it using _ArrayBinarySearch() and _ArraySearch()


A decision is a powerful thing

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  
Followers 0