emendelson Posted May 17, 2006 Share Posted May 17, 2006 Part of my script opens and file and looks for a string, like this (adapted from the help file): ;; read text until end of file or match found $gstxt = FileOpen("filename.txt", 0) While 1 $gsline = FileReadLine($gstxt) If StringInStr($gsline, "some string") Then ExitLoop EndIf If @error = -1 Then ExitLoop WEnd FileClose($gstxt) What I can't figure out is: how do I determne if "some string" was never found? This will be obvious to just about everyone here, but all the things I've tried haven't solved the problem. Thanks for any help. Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted May 17, 2006 Share Posted May 17, 2006 seriously tell me now why the crap are you do a filereadline and your not defining a line to read , THATS THE ENTIRE POINT OF THE FUNCTION but ok in my code if it finds it then it returns the occurance correct? well if its 0 then there arent any so it will @erorr 0 but if you say for success it has to be great then 0 then yeah it will be found $gstxt = FileOpen("filename.txt", 0) While 1 $line = StringInStr(FileRead($gstxt), "some string") If $line > 0 Then MsgBox(0,"Found","The line Some String was found" & @CRLF & "If Was found " & $line & " time(s)") FileClose($gstxt) Exit Elseif @error = 0 Then MsgBox(0,"Not Found","Sorry the string was not found") Exit EndIf WEnd Link to comment Share on other sites More sharing options...
MHz Posted May 17, 2006 Share Posted May 17, 2006 You need to check error right after a function call to check it, else it is reset with the next function call. Just use a variable to flag if the string was found as $stringfound does. $gstxt = FileOpen("filename.txt", 0) If $gsline <> -1 Then $stringfound = 0 While 1 $gsline = FileReadLine($gstxt) If @error Then ExitLoop If StringInStr($gsline, "some string") Then $stringfound = 1 ExitLoop EndIf WEnd If $stringfound Then MsgBox(0x40, '', 'String was found') Else MsgBox(0x30, '', 'String was not found') EndIf FileClose($gstxt) Else MsgBox(0x10, '', 'FileOpen error') EndIf Link to comment Share on other sites More sharing options...
emendelson Posted May 17, 2006 Author Share Posted May 17, 2006 (edited) You need to check error right after a function call to check it, else it is reset with the next function call. Just use a variable to flag if the string was found as $stringfound does. $gstxt = FileOpen("filename.txt", 0) If $gsline <> -1 Then $stringfound = 0 While 1 $gsline = FileReadLine($gstxt) If @error Then ExitLoop If StringInStr($gsline, "some string") Then $stringfound = 1 ExitLoop EndIf WEnd If $stringfound Then MsgBox(0x40, '', 'String was found') Else MsgBox(0x30, '', 'String was not found') EndIf FileClose($gstxt) Else MsgBox(0x10, '', 'FileOpen error') EndIf Thanks for that - I managed to put the various tests in the wrong place, and got messed up. And thanks also to thatsgreat2345 - before this, I simply didn't know how to use FileRead to do this job, and now I do! It all looks obvious once I see it in the code, but I'm still getting my feet wet with AutoIt, and didn't understand it before... Thanks again. Edited May 17, 2006 by Edward Mendelson Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted May 17, 2006 Share Posted May 17, 2006 (edited) as i said FileRead lol reads the entire file i mean if you dont define any number for filereadline then you know it still reads its just kinda like idk whatever that people are going to all that trouble to type LINE hahaha Edited May 17, 2006 by thatsgreat2345 Link to comment Share on other sites More sharing options...
emendelson Posted May 17, 2006 Author Share Posted May 17, 2006 (edited) as i said FileRead lol reads the entire file i mean if you dont define any number for filereadline then you know it still reads its just kinda like idk whatever that people are going to all that trouble to type LINE hahahaWait - I didn't ask my question correctly. What I need to do is read a line that says something like this:"The answer is " <a string I don't know in advance>I don't have any way of knowing in advance what line number that will be.So it's not enough to read the file and find the string "The answer is " - I also need to get the rest of the line that BEGINS "The answer is ". Is there a way that I can use FileRead to do that? All I could figure out from the help is that FileRead will tell me how many times "The answer is " is present in the file. But that doesn't tell me *what* "The answer is "... Edited May 17, 2006 by Edward Mendelson Link to comment Share on other sites More sharing options...
herewasplato Posted May 17, 2006 Share Posted May 17, 2006 Just a slight change from the code in the help file under FileReadLine: $file = FileOpen("test.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, "some string") Then ExitLoop WEnd MsgBox(0, "Line read:", $line) FileClose($file)You could combine the If @error and the stringinstr onto one line. [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
herewasplato Posted May 17, 2006 Share Posted May 17, 2006 ...if you dont define any number for filereadline then you know it still reads......but it will only read one line of the file, not the whole file... [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted May 17, 2006 Share Posted May 17, 2006 (edited) but if you don't define a number count of lines it reads the entire document yes ? no? HAHAHA ok I'm terribly sorry im fighting a lost cause i didn't know it only read the first line if no line number was defined Edited May 17, 2006 by thatsgreat2345 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 17, 2006 Moderators Share Posted May 17, 2006 I think if you are looking for specific text, you are doing yourself an injustice not using just a 1 line simple code with StringRegExp() (Beta). This would ultimately speed up any search to many times faster than what your currently doing. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
herewasplato Posted May 17, 2006 Share Posted May 17, 2006 but if you don't define a number count of lines it reads the entire document yes ? no?...I should have posted this quote from the help file under FileReadLine. I thought about it, but didn't post it the first time:RemarksReturns the text of the line read, any newline characters ( CHR(10) or @LF ) at the end of a line read in are automatically stripped.If no line number to read is given, the "next" line will be read. ("Next" for a newly opened file is initially the first line.) [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted May 17, 2006 Share Posted May 17, 2006 yeah i was checking the help file as well but when i typed filereadline it only went to fileread since i didnt capitalize the L :-/ Link to comment Share on other sites More sharing options...
herewasplato Posted May 17, 2006 Share Posted May 17, 2006 ...just a 1 line simple code with StringRegExp() (Beta)...If the string to search for was "some string" and it was in the file more than once, could you do that in one line?text in:this is some stringthis is also some stringsome other lineyet another linethe third line with some stringtext out:this is some stringthis is also some stringthe third line with some stringjust wondering... [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
herewasplato Posted May 17, 2006 Share Posted May 17, 2006 ...i didnt capitalize the L :-/Wild, I have never noticed that before - and I have to use the help file a lot! [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 17, 2006 Moderators Share Posted May 17, 2006 If the string to search for was "some string" and it was in the file more than once, could you do that in one line?text in:this is some stringthis is also some stringsome other lineyet another linethe third line with some stringtext out:this is some stringthis is also some stringthe third line with some stringjust wondering...Depends on what specifically you were looking for, but anything your going to do with FileRead-For/Next-StringInStr, I would think it would just be easier to do FileRead-StringRegExp(), they are going to both follow the same rules basically, and if you need to set others than you can manipulate it as need be, I just think it's easier to return an array of all "Some Strings" in a timely manner, and parse from there than to go through each Line individually to accomplish the same thing. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
emendelson Posted May 17, 2006 Author Share Posted May 17, 2006 (edited) MHz's method works perfectly in one of my scripts, but fails in another one, and I've spent an hour trying to figure out why. But I can't, and I'll be grateful for any help. Here is the script: it downloads a web page and searches for a string that it can later parse as the current Ghostscript version. If the script finds the string, it works perfectly. If it doesn't, it simply exits without an error message. And that's what's really baffling me, because the identical routine works perfectly (posts error messages, etc.) in a different script. Here goes. The script should work on anyone's system; uses beta. The way I test it is by UNcommenting the line that tests for the string "You won't find this in the file" and COMMENTING the line that tests for the actual string. expandcollapse popupDim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $e = InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) ;If $e = 0 Then ; MsgBox(8298, $t, "Error: Unable to test Ghostcript version. Install Ghostscript manually and rerun this program.") ; Exit ;EndIf GetCurGSVer() Exit Func GetCurGSVer() $gstxt = FileOpen($gshtml, 0) If $gsline <> - 1 Then $stringfound = 0 While 1 $gsline = FileReadLine($gstxt) If @error Then ExitLoop ;If StringInStr($gsline, "You won't find this in the file.") Then If StringInStr($gsline, "<li><b><a href=" & Chr(34) & "doc/AFPL/get") Then $stringfound = 1 ExitLoop EndIf If @error = -1 Then ExitLoop WEnd MsgBox(0, $t, $stringfound) If $stringfound <> 1 Then MsgBox(8298, $t, "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf FileClose($gstxt) Else MsgBox(8298, $t, "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf FileDelete($gshtml) MsgBox(0, $t, $gsline) EndFunc;==>GetCurGSVer If anyone can see what's wrong, I'll be very grateful. Edited May 17, 2006 by Edward Mendelson Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 17, 2006 Moderators Share Posted May 17, 2006 At a glance I would say your @error checking is wrong:expandcollapse popupDim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $e = InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) ;If $e = 0 Then ; MsgBox(8298, $t, "Error: Unable to test Ghostcript version. Install Ghostscript manually and rerun this program.") ; Exit ;EndIf GetCurGSVer() Exit Func GetCurGSVer() $gstxt = FileOpen($gshtml, 0) If $gsline <> - 1 Then $stringfound = 0 While 1 $gsline = FileReadLine($gstxt) If @error = -1 Then ExitLoop If StringInStr($gsline, "You won't find this in the file.") Then ;If StringInStr($gsline, "<li><b><a href=" & Chr(34) & "doc/AFPL/get") Then $stringfound = 1 ExitLoop EndIf WEnd MsgBox(0, $t, $stringfound) If $stringfound <> 1 Then MsgBox(8298, $t, "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf FileClose($gstxt) Else MsgBox(8298, $t, "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf FileDelete($gshtml) MsgBox(0, $t, $gsline) EndFunc ;==>GetCurGSVer Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
emendelson Posted May 17, 2006 Author Share Posted May 17, 2006 At a glance I would say your @error checking is wrong:expandcollapse popupDim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $e = InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) ;If $e = 0 Then ; MsgBox(8298, $t, "Error: Unable to test Ghostcript version. Install Ghostscript manually and rerun this program.") ; Exit ;EndIf GetCurGSVer() Exit Func GetCurGSVer() $gstxt = FileOpen($gshtml, 0) If $gsline <> - 1 Then $stringfound = 0 While 1 $gsline = FileReadLine($gstxt) If @error = -1 Then ExitLoop If StringInStr($gsline, "You won't find this in the file.") Then ;If StringInStr($gsline, "<li><b><a href=" & Chr(34) & "doc/AFPL/get") Then $stringfound = 1 ExitLoop EndIf WEnd MsgBox(0, $t, $stringfound) If $stringfound <> 1 Then MsgBox(8298, $t, "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf FileClose($gstxt) Else MsgBox(8298, $t, "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf FileDelete($gshtml) MsgBox(0, $t, $gsline) EndFunc;==>GetCurGSVer You're right that my error checking was wrong, but unfortunately this one doesn't work either. It simply exits without an error message if the string isn't found. Would someone be willing to run the script (it takes only a second or two) and let me know if it works on anyone else's system. As I said, there's no problem if the script finds the string. But it simply exits if it doesn't find the string. Many thanks Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 17, 2006 Moderators Share Posted May 17, 2006 (edited) This worked for me, maybe you can mod it:Dim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $FindString1 = "<li><b><a href=" & Chr(34) & "doc/AFPL/get" If InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) Then If _GetCurGSVer($gshtml, $FindString1) Then MsgBox(64, 'Info:', 'Your String Was Found') Else MsgBox(8298, "TestFTP", "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf EndIf Func _GetCurGSVer($hFileRead, $s_Find) $hFileRead = FileRead($hFileRead, FileGetSize($hFileRead)) If StringInStr($hFileRead, $s_Find) Then FileDelete($gshtml) Return 1 EndIf FileDelete($gshtml) Return 0 EndFunc Edit: Or even this one that returns the actual Version number at the same time:Dim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $FindString1 = "<li><b><a href=" & Chr(34) & "doc/AFPL/get" If InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) Then $Version = _GetCurGSVer($gshtml, $FindString1, '.htm') If $Version Then MsgBox(64, 'Info:', 'Your String Was Found' & @CR & 'And your Version is: ' & $Version) Else MsgBox(8298, "TestFTP", "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf EndIf Func _GetCurGSVer($hFileRead, $s_Start, $s_End) $hFileRead = FileRead($hFileRead, FileGetSize($hFileRead)) $aArray = StringRegExp($hFileRead, '(?:' & $s_Start & ')(.*?)(?:' & $s_End & ')', 3) If Not @error Then FileDelete($gshtml) Return $aArray[0] EndIf FileDelete($gshtml) Return 0 EndFunc Edited May 17, 2006 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
emendelson Posted May 17, 2006 Author Share Posted May 17, 2006 (edited) This worked for me, maybe you can mod it:Dim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $FindString1 = "<li><b><a href=" & Chr(34) & "doc/AFPL/get" If InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) Then If _GetCurGSVer($gshtml, $FindString1) Then MsgBox(64, 'Info:', 'Your String Was Found') Else MsgBox(8298, "TestFTP", "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf EndIf Func _GetCurGSVer($hFileRead, $s_Find) $hFileRead = FileRead($hFileRead, FileGetSize($hFileRead)) If StringInStr($hFileRead, $s_Find) Then FileDelete($gshtml) Return 1 EndIf FileDelete($gshtml) Return 0 EndFunc Edit: Or even this one that returns the actual Version number at the same time:Dim $gsline, $stringfound, $gstxt, $gshtml, $gsinst, $t $t = "TestFTP" $gshtml = @TempDir & "\gsver.txt" $gsinst = @TempDir & "\gsinst.exe" $FindString1 = "<li><b><a href=" & Chr(34) & "doc/AFPL/get" If InetGet("http://www.cs.wisc.edu/~ghost/", $gshtml, 1, 0) Then $Version = _GetCurGSVer($gshtml, $FindString1, '.htm') If $Version Then MsgBox(64, 'Info:', 'Your String Was Found' & @CR & 'And your Version is: ' & $Version) Else MsgBox(8298, "TestFTP", "Ghostscript version could not be determined. Install Ghostscript manually and rerun this program.") Exit EndIf EndIf Func _GetCurGSVer($hFileRead, $s_Start, $s_End) $hFileRead = FileRead($hFileRead, FileGetSize($hFileRead)) $aArray = StringRegExp($hFileRead, '(?:' & $s_Start & ')(.*?)(?:' & $s_End & ')', 3) If Not @error Then FileDelete($gshtml) Return $aArray[0] EndIf FileDelete($gshtml) Return 0 EndFunc Thanks for these.. The first one works if the string is in the file, but fails in the same way that mine fails if you search for a string that is NOT in the file. It simply exits - no error message, nothing. I also tried the second one - and it works beautifully and elegantly to return the string. But it fails with a syntax error if you search for a string that isn't found in the file. Try replacing the $FindString1 variable with something that isn't going to be found in the file, and you'll get an error message in line 23: (Return $aArray[0]) Subscript used with non-array variable. I'm trying to trap a situtation where the string isn't found, and it's surprisingly frustrating to get it working. Many thanks for your help with this. Edited May 17, 2006 by Edward Mendelson Link to comment Share on other sites More sharing options...
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