AutoHotkey Community

It is currently May 25th, 2012, 1:09 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: July 26th, 2007, 3:06 pm 
Offline

Joined: January 13th, 2006, 3:13 pm
Posts: 29
Hi all. I am trying to work out a problem I am having with some code. It's supposed to search a text file, display what is found in a gui, and then exit.

That does work, but I noticed it wasn't doing multiple searches, so I am attempting to fix it so it will redisplay multilpe found search patterns. Here is my code so far:
Code:
Found := 0

FiletoRead = C:\Temp\File.txt

Gui, Font, S10 CDefault, Verdana
Gui, add, text,,Enter the Search Pattern:
Gui, add, edit, vPattern,

Gui, add, button, default, OK
Gui, show,, Word searcher
Return 

GuiEscape:
GuiClose:
   Gui, Destroy
   ExitApp

ButtonOK:
   Gui, Submit
   Gui, Destroy

Loop, Read, %FiletoRead%
{
   IfInString, A_LoopReadLine, %Pattern%
   {
      Found := 1
      Gui, 2:Font, S12 CDefault, Courier New
      Gui, 2:add, text,Center,%A_LoopReadLine%
      Gui, 2:show,, Password searcher
      Return
        2GuiEscape:
        2GuiClose:
          Gui, Destroy

      Return
   }
   
}

If not Found
   {
   MsgBox, 48,Warning - Word searcher, Nothing Found!
   ExitApp
   }

ExitApp

I am having trouble with the IfInString portion of the loop. I can't seem to get it to return out of that section, to continue to search the file. It errors off saying I need a Return before the bracket. But if I put a Return there it seems to return back to gui, 2.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 4:09 pm 
Offline

Joined: July 23rd, 2007, 3:43 am
Posts: 47
Try this..
Code:
; Found := 0    ;<= not needed since var's start out blank..
FiletoRead = C:\Temp\File.txt

Gui, Font, S10 CDefault, Verdana
Gui, add, text,,Enter the Search Pattern:
Gui, add, edit, vPattern,
Gui, add, button, default, OK
Gui, show,, Word searcher
Return

GuiEscape:
GuiClose:
   Gui, Destroy
   ExitApp

ButtonOK:
  Gui, Submit
  Gui, Destroy

  Loop, Read, %FiletoRead%
  {
    If InStr(A_LoopReadLine, Pattern) ;--if found.. add to 'Found' list..
      Found .= Found ? "`n" . A_LoopReadLine | A_LoopReadLine
  }

  If Found  {
    Gui, 2:Font, S12 CDefault, Courier New
    Loop, Parse, Found, `n    ;--add each line in 'Found' as a gui text..
      Gui, 2:add, text,Center,%A_LoopField%

    Gui, 2:show,, Password searcher   ;--show your new gui..
    Return
  } Else    {
    MsgBox, 48,Warning - Word searcher, Nothing Found!
    ExitApp
  }

2GuiEscape:
2GuiClose:
  Gui, Destroy
  ExitApp
Notice how it doesn't show the new gui until after it adds all the matches.

Here is another example that uses one gui to show all the results in a ListView control.
Code:
FileRead, FileVar, C:\Temp\File.txt   ;--read to var..

Gui, Font, S10 CDefault, Verdana
Gui, Add , Text    ,                 , Enter the Search Pattern:
Gui, Add , Edit    , Section gPattern,
Gui, Add , ListView, xs      vResults, Results
Gui, Show,                           , Word searcher
Return

Pattern:
  LV_Delete()       ;--clears previous results..
  If !A_GuiControl  ;--(A_GuiControl contains contents of edit field..)
    return
 
  Loop, Parse, FileVar, `n, `r
    If RegExMatch( A_LoopField, "^" . A_GuiControl )  ;-adds matches to Listview..
      LV_Add("",  A_LoopField )
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 5:07 pm 
Offline

Joined: January 13th, 2006, 3:13 pm
Posts: 29
the first one looks good, but it isn't finding anything.

I take it
Code:
      Found .= Found ? "`n" . A_LoopReadLine | A_LoopReadLine
may be a little off.

EDIT: I changed it to
Code:
     Found .= Found ? "`n" . A_LoopReadLine : A_LoopReadLine
and that works now.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 5:28 pm 
Offline

Joined: July 23rd, 2007, 3:43 am
Posts: 47
Whoops!! :oops: Sorry about that...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 5:33 pm 
Offline

Joined: January 13th, 2006, 3:13 pm
Posts: 29
no biggie. :) Thanks for the assistance


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Amandaville, Bing [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group