Sign in to follow this  
Followers 0
GoogleDude

Search for string in TXT file and set string as $var?

11 posts in this topic

Sorry if this question has been asked before but! I searched the Help and all I came up with is "StringInStr" But I wasnt sure if that is what I am looking for or not?

How can I search for a string of text in a TXT file and set the found string as a $var?

Sorry for the beginner questions!

GoogleDude

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

well, about the setting $var as the found string would be useless since you already know what the string you are searching for and "hoping" to return anyways.

but here is a way it can be done.

#include <file.au3>
#include <array.au3>
Func _StringInFile($file, $string)
    Local $avLines[_FileCountLines($file)]
    _FileReadToArray($file, $avLines)
    For $i = 1 to $avLines[0]
        if (StringInStr($avLines[$i], $string)) Then
            Return True
        Else
        EndIf
    Next
    Return False
EndFunc
Edited by CHRIS95219

Share this post


Link to post
Share on other sites

Thanks for the fast reply!

Well the thing is the string may vary, IE... I am looking for any word that contains the word "test" kinda like a wildcard search! so it may return "testing123" and if it does then "testing123" should be set as the var.

Does that change anything?

GoogleDude

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Func _StringInFile($file, $string)
    Local $avLines[_FileCountLines($file)]
    _FileReadToArray($file, $avLines)
    For $i = 1 to $avLines[0]
        if (StringInStr($avLines[$i], $string)) Then
            $words = StringSplit($avLines[$i], " ")
            for $x = 1 to $words[0]
                if (StringInStr($words[$x], $string)) Then
                    Return $words[$x]
                Else
                EndIf
            Next
        Else
        EndIf
    Next
    Return False
EndFunc

This work?

Edited by CHRIS95219

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks a ton, but one more dumb/noob question. Where do I put my "string" to search for and the "TXT file" and location to search?

Thanks again for your prompt response. I hope some day I can be as helpfull as you and the others on this forum.

GoogleDude

Edited by GoogleDude

Share this post


Link to post
Share on other sites

Well HooRahh for me. Thanks to you! That did just the trick.

Thanks again.

GoogleDude

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

What's the sense of _FileCountLines() if you are using _FileReadToArray()?

Edit:

Sorry... this is what I meant didn't make sense:

Func _StringInFile($file, $string)
    Local $avLines[_FileCountLines($file)]
    _FileReadToArray($file, $avLines)
    For $i = 1 to $avLines[0]
        if (StringInStr($avLines[$i], $string)) Then
            $words = StringSplit($avLines[$i], " ")
            for $x = 1 to $words[0]
                if (StringInStr($words[$x], $string)) Then
                    Return $words[$x]
                Else
                EndIf
            Next
        Else
        EndIf
    Next
    Return False
EndFunc

This work?

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Sorry again for the NOOB question AGAIN, but I am having a problem adding the "string" to a RunWait command!

This is what I am trying but it isnt working. The SC window pops up but its saying "wrong arguments".

RunWait(@ComSpec & " /c " & 'sc delete' & $var )

Am I missing the correct syntex for the RunWait function?

Also, Can you put a Function inside a Function?

Thanks again.

GoogleDude

Share this post


Link to post
Share on other sites

Try a space after delete.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

What's the sense of _FileCountLines() if you are using _FileReadToArray()?

Edit:

Sorry... this is what I meant didn't make sense:

because _FileReadToArray does'nt return an array, you have to pass the "already made" array by reference.

and so the array is the right size that _FileReadToArray will be using...?

btw, I don't like declaring arrays without a set element amount(unless absolutely unneeded), helps me feel more organized... :)

Edited by CHRIS95219

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