Sign in to follow this  
Followers 0
flinchy

Does Text Exists In A File?

3 posts in this topic

#1 ·  Posted (edited)

Hey all im new here but im finding Autoit pretty easy to pick up and the only thing i cant do is something i think should be easy compared to other stuff ive done with it...

Is there a way to search through a file for some text and if it isnt there then it is written...

I know that the FileWriteLine("file here", @LF & "Text to add to the end") is needed... But i cant work out the If command for the rest...

I have tried

If FileReadLine($hostPath & "\hosts", "127.0.0.1            facebook.com") = false Then
   FileWriteLine($hostPath & "\hosts", @LF & "127.0.0.1            facebook.com")
End If

but it doesnt work >_<

any help?

Cheers Flinchy

Edit:

The finding of "127.0.0.1 facebook.com" can be ANYWHERE in the file as long as it doesnt have a # in front of it but if it doesnt exist it is added to the end...

Edited by flinchy

Share this post


Link to post
Share on other sites



$filetext = FileRead($filepath)

If Not StringInStr($filetext, $textyourlookingfor) Then
    FileWriteLine($hostPath & "\hosts", @LF & "127.0.0.1            facebook.com")
EndIf

Just wrote this real quick, so it may not work, but it should give you the general idea of what you need to do.


The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

$filetext = FileRead($filepath)

If Not StringInStr($filetext, $textyourlookingfor) Then
    FileWriteLine($hostPath & "\hosts", @LF & "127.0.0.1            facebook.com")
EndIf

Just wrote this real quick, so it may not work, but it should give you the general idea of what you need to do.

Thanks ill give it a go now and let you know how it works out >_<

Edit: It works perfectly thanks alot :(

Edited by flinchy

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