Jump to content

Find text in file


Nova
 Share

Recommended Posts

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 ?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Developers

[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)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@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
Link to comment
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?

Link to comment
Share on other sites

  • Developers

@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 ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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}>

Link to comment
Share on other sites

  • Developers

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 ?)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...