Jump to content

Find String


nobby
 Share

Recommended Posts

One way is to read in the entire file using FileReadLine--see help file for example.

Another way shown below is to use the command-line tool find and pipe the results to a file....

Not tested!

$word = "banana"
$fileToSearch = '"C:\wherever\fruit.txt"'

RunCmd('find /n ' & $word & ' ' & $fileToSearch & ' ' > "output.txt")

;RunCmd creates a file output.txt with the search results.
;  Output.txt's first line is something like "----- FRUIT.TXT"
;  If other lines are present, they tell you where in FRUIT.TXT
;  the word 'banana' was found

FileOpen("output.txt", 0)

; rest of script goes here

Func RunCmd( $command )
  Return RunWait( @ComSpec & " /C " & $command, "", @SW_HIDE )
EndFunc
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Thanks for the replies.

I was hoping to stay away from DOS and other external apps.

I have tested the code and found that it does not work because the quote marks around the variable are not quite correct

$word = "banana"
$fileToSearch = '"C:\wherever\fruit.txt"'
Should be
$word = '"banana"'
$fileToSearch = "c:\wherever\fruit.txt"

For the FIND arguments to be formatted correctly.

But there's more and this is when it get a bit weird...

Here is a code that is a slight variant from the original

$word = '"banana"'
$fileToSearch = "C:\fruit.txt"

RunCmd('find /n ' & $word & ' ' & $fileToSearch & ' ' > "C:\output.txt")

;RunCmd creates a file output.txt with the search results.
;  Output.txt's first line is something like "----- FRUIT.TXT"
;  If other lines are present, they tell you where in FRUIT.TXT
;  the word 'banana' was found

FileOpen("c:\output.txt", 0)

; rest of script goes here

Func RunCmd( $command )
 Return RunWait( @ComSpec & " /C " & $command, "", @SW_HIDE )
EndFunc

When running this bit of code with notepad open and focussed, my logon password gets typed into notepad???

This happens only when the output of find is piped into a filename with a path. If it is set just to "output.txt" the script does not create the output.txt file, but it does not type the logon password into notepad.

Note that my logon password may also have been included into the IE password manager at some stage.

Could someone try the script and see if the result is the same???

Cheers :whistle:

CheersNobby

Link to comment
Share on other sites

All AutoIt solution:

$word=InputBox("Question", "What word?", "banana", "", -1, -1, 0, 0)
$file = FileOpen("fruit.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
if StringInStr(" "&$line,$word)>0 then MsgBox(1, "Found"& $word, $line) 
; I add the space in front in case banana is the first word of that line. 
Wend
FileClose($file)

Easy to get more complex though, you could count the amount of times, ect.

edit... actually this is almost entirely taken from the help file as CyberSlug pointed to. I tend to use the help file for examples whenever I can. :whistle:

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

You can run it from anouther script if you like, or even a command line as well.

; search.au3
; usage search.exe filename searchword
$count=0
if $CmdLine[0]=2 then
$word=$CmdLine[2]
$file = FileOpen($CmdLine[1], 0)
If $file = -1 Then
   Exit
EndIf
While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
if StringInStr(" "&$line,$word)>0 then 
$count=$count+1
ToolTip ( "Found "&$word&" in "&$line , 0, 0 )
sleep(3000)
endif
Wend
FileClose($file)
endif

You can also use this as a function, and it shouldn't gain focus unless you use a popup msgbox, or input box.

edit... put sleep inside if so that it only shows tip if it finds something.

and showed a use of count.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

I'm trying to parse a file that has a lot of...how you say..."junk" in it. Lots of those "square like" characters mixed in with the stuff I am trying to read.

The end product is that I am trying to count the occurences of certain strings found w/in the file. The file is in essence one line only and approximately 4300-4600 bytes. When I read in the line, only the first 100 or so characters appear if I output my variable to a file. However, when I put in a line break in this one-line file(around the 100th character)and make it two lines, I can read all the second-line characters (about 4400 of them) with no problem.

Is there a command that will let me start "reading" from a particular byte of the file, so I can just skip all the "junk" in the beginning?

I know this is possible using a file-pointer in C, however I'd like to stick to an AutoIt only solution if at all possible. I also am trying to do this w/o losing focus on the current window (all parsing of the file should be done in the background).

Any help/advice would be appreciated!

Link to comment
Share on other sites

$file = FileOpen("fruit.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop
$x=line; the one line you have
;msgbox(1,"Displaying the line",StringTrimLeft ( $x, 100 ); uncomment this if you have more than one line
Wend
FileClose($file)
msgbox(1,"Displaying the line",StringTrimLeft ( $x, 100 ); show everything after the 100th character.

You can also determine what the little box is with many different programs, such as UltraEdit in hex mode. then you can simply parse out the data with chr() or simply remove any text that is not in your range.

AutoIt has many tools for working with strings, one of them is sure to work.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

This should give you a straight count of what ever you wish. It grabs each line, finds if it has the word, removes it from the front of the line, and tests again until it can't find it, and then does the next line till the end of the file.

It displays the result in the top left corner for 3 seconds or so, and puts it in the clipboard, yet does not change focus.

; search.au3
; usage search.exe filename searchword
$count=0
if $CmdLine[0]=2 then
$word=$CmdLine[2]
$file = FileOpen($CmdLine[1], 0)
If $file = -1 Then
  Exit
EndIf
While 1
  $line = FileReadLine($file)
  If @error = -1 Then ExitLoop
while StringInStr(" "&$line,$word)>0
$count=$count+1
$line=StringTrimLeft ( $line, StringInStr(" "&$line,$word) )
wend

Wend
FileClose($file)
endif
ToolTip ( "Found "&$word&" in "&$count&" times", 0, 0 )
sleep(2000)
clipput($count)

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

  • 3 years later...

Please help me:

$file = FileOpen("test.txt", 0)

$word=InputBox("Question", "What word?", "", "", -1, -1)

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

if StringInStr(" "&$line,$word)>0 then MsgBox(1, "Found"& $word, $line)

Wend

ClipPut($line)

i need to copy $line into clipboard

tank

Link to comment
Share on other sites

i need to copy $line into clipboard

So where the problem? you did it in the code. Or you want to put the line number?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Not work, $line is empty

tank

testing...

$file = FileOpen(@ScriptFullPath, 0)
$word=InputBox("Question", "What word?", "", "", -1, -1)

$sWFound= ''
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr(" "&$line,$word) > 0 Then
        $sWFound &= $line & @LF
        MsgBox(1, "Found"& $word, $line)
    EndIf
Wend

ClipPut($sWFound)
Link to comment
Share on other sites

testing...

$file = FileOpen(@ScriptFullPath, 0)
$word=InputBox("Question", "What word?", "", "", -1, -1)

$sWFound= ''
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr(" "&$line,$word) > 0 Then
        $sWFound &= $line & @LF
        MsgBox(1, "Found"& $word, $line)
    EndIf
Wend

ClipPut($sWFound)
Tank , it work fine !
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...