Sign in to follow this  
Followers 0
Nova

Find text in file

12 posts in this topic

Ok im still working on a solution for the same problem I posted about 2 days ago,ive been real busy and just found a few spare mins to give it another try !

The following code pings comp4 on my home network (Comp4 dose not exist) and saves the results into a txt file called Temp.txt

Then it open temp.txt and reads the file

Then it looks for the text "Ping request could not find host comp4" (I know that this text has to be present in the file Temp.txt because u get this reply when u ping a computer that dosent exist )

Problem code always returns "Text not found" !

RunWait(@ComSpec & " /c " & 'ping comp4 >> C:/Temp.txt', "", @SW_HIDE)
$file = FileOpen("C:/Temp.txt", 0)

   If $file = -1 Then
      MsgBox(0, "Error", "Unable to open file.")
      Exit
   EndIf

   $Find = StringInStr( $file, "Ping request could not find host comp4" ) 

   If $Find = 0 Then
      MsgBox(0, "Error", "Text not found")
   Else
      MsgBox(0, "Error", "Text found")      
   EndIf

FileClose($file)
Exit 0

Im asuming that the line

$Find = StringInStr( $file, "Ping request could not find host comp4" )

dosent actually search $file for the txt "ping request could not find host comp4"

Anyone point out what im doing wrong ?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I am not sure you were joking or not, but you where looking for text in the file handle... :idiot:

RunWait(@ComSpec & " /c " & 'ping comp4 >> C:/Temp.txt', "", @SW_HIDE)
$file = FileRead("C:/Temp.txt", FileGetSize("C:/Temp.txt"))
FileDelete('c:\temp.txt')

  If @error Then
     MsgBox(0, "Error", "Unable to open file.")
     Exit
  EndIf

  $Find = StringInStr( $file, "Ping request could not find host comp4" ) 

  If $Find = 0 Then
     MsgBox(0, "Error", "Text not found")
  Else
     MsgBox(0, "Error", "Text found")      
  EndIf

;FileClose($file) <-Sorry. Forgot. Still the mistake is YOURS.
Exit 0
Edited by ezzetabi

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Damit, I just opened the file, I never read it !

Cheers ezzetabi !

There was one line of code u wrote and I dont understand why its there !

FileGetSize("C:/Temp.txt")

Why add this in I dont c a use in this case for it ?

EDIT:Figured it out second parameter in file read is count (The number of characters to read) so using file get size as count tells file read to read the whole file.

Edited by nova

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

FileClose($file)

[Payback]I am not sure you were joking or not, but you tryed to close a file handle ! :idiot:[/payback]

When ur script is ran u get an error

Error invalid file handle used

Edited by nova

Share this post


Link to post
Share on other sites

[Payback]I am not sure you were joking or not, but you tryed to close a file handle !  :idiot:[/payback]

When ur script is ran u get an error

<{POST_SNAPBACK}>

just remove this line:

FileClose($file)

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@Jedb I realised the problem with ezzetabi code and quickly fixed it !

I woz just messn with him coz he pointed out my stupid mistake above with his comment

I am not sure if you were joking or not, but you where looking for text in the file handle...  :idiot:

So I commented on his stupid mistake with

I am not sure if you were joking or not, but you tryed to close a file handle !  :D

Twoz all in good fun

Any ways the following works at least 10 times quicker when trying to figure out if a given computer is online !

RunWait(@ComSpec & " /c " & 'net view >> C:/Temp.txt', "", @SW_HIDE)
$file = FileRead("C:/Temp.txt", FileGetSize("C:/Temp.txt"))

If @error Then
   Exit
EndIf

$Find = StringInStr( $file, "\\Comp4" ) 

 If $Find1 = 0 Then
   MsgBox(0, "Info", "Comp4 is offline")
 Else
   MsgBox(0, "Info", "Comp4 is online")      
 EndIf

FileDelete('c:\temp.txt')
Exit 0
Edited by nova

Share this post


Link to post
Share on other sites

Actually I remember you that the faulty script that tried to look for a string inside a filehandle is yours.

I just forgot to remove that useless line.

@Jbed, if I copy the script of my post in a scite buffer and press ALT+9 it does not actually tidy.

Sometime certain lines, already wrongly indented, are ignored, it is just me?

Share this post


Link to post
Share on other sites

@Jbed, if I copy the script of my post in a scite buffer and press ALT+9 it does not actually tidy.

Sometime certain lines, already wrongly indented, are ignored, it is just me?

<{POST_SNAPBACK}>

can you give me an example script that doesn't tidy correctly ?

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

As I said.

RunWait(@ComSpec & " /c " & 'ping comp4 >> C:/Temp.txt', "", @SW_HIDE)
$file = FileRead("C:/Temp.txt", FileGetSize("C:/Temp.txt"))
FileDelete('c:\temp.txt')

  If @error Then
     MsgBox(0, "Error", "Unable to open file.")
     Exit
  EndIf

  $Find = StringInStr( $file, "Ping request could not find host comp4" ) 

  If $Find = 0 Then
     MsgBox(0, "Error", "Text not found")
  Else
     MsgBox(0, "Error", "Text found")      
  EndIf

;FileClose($file) <-Sorry. Forgot. Still the mistake is YOURS.
Exit 0

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

As I said.

<{POST_SNAPBACK}>

Works fine for me when i run tidy on this code ....

What settings do you have ? (can you show/PM me your Tidy.ini info ?)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Whats this tidy command thing, i think i need it.

I always leave stupid things


War does not determine who is right, it determines who is left.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

JdeB improved SciTe have a integrated command to tidy the scrips.

I think can find also stand alone version in his pages.

http://www.autoitscript.com/fileman/users/jdeb/jdeb_autoit_stuff.htm

Edited by ezzetabi

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