nobby Posted February 24, 2004 Posted February 24, 2004 Hey, Is there a way of finding a word within a text file? Say I am trying to look for the word "banana" in fruit.txt, then do something based uppon the result? Cheers CheersNobby
CyberSlug Posted February 24, 2004 Posted February 24, 2004 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!
trids Posted February 24, 2004 Posted February 24, 2004 You've also got the option of using Windows' grep .. along the lines of Sluggies description of the DOS find. It offer Regular Expressions too
nobby Posted February 25, 2004 Author Posted February 25, 2004 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 CheersNobby
CyberSlug Posted February 25, 2004 Posted February 25, 2004 Oops. It appears the redirection/pipe thingy was not inside qutation marks. Try: RunCmd('find /n ' & $word & ' ' & $fileToSearch & " > output.txt") Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
scriptkitty Posted February 25, 2004 Posted February 25, 2004 (edited) 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. Edited February 25, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers.
stfields Posted February 25, 2004 Posted February 25, 2004 ScriptKitty: If I remove all the pop-up msg boxes from your code, will it change the "window focus" at any time when searching for a string in that file? Also on a non-related topic: does the FileDelete command cause the window focus to change?
scriptkitty Posted February 25, 2004 Posted February 25, 2004 (edited) 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 February 25, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers.
stfields Posted February 25, 2004 Posted February 25, 2004 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!
scriptkitty Posted February 25, 2004 Posted February 25, 2004 $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.
scriptkitty Posted February 25, 2004 Posted February 25, 2004 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.
Josbe Posted February 26, 2004 Posted February 26, 2004 ...and for a search more specific, use Case-sensitive. AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta
maurocav Posted March 16, 2007 Posted March 16, 2007 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
MrCreatoR Posted March 16, 2007 Posted March 16, 2007 i need to copy $line into clipboardSo 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 Russian Community My Work... Spoiler Projects: 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 ProgramUDFs: 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 Examples: 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 ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team
maurocav Posted March 16, 2007 Posted March 16, 2007 So where the problem? you did it in the code. Or you want to put the line number?Not work, $line is emptytank
Josbe Posted March 16, 2007 Posted March 16, 2007 Not work, $line is empty tanktesting... $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) AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta
maurocav Posted March 17, 2007 Posted March 17, 2007 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 !
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now