caspermurphy Posted August 15, 2005 Share Posted August 15, 2005 Good day, I am trying to open a text file find some text, do a carriage return to new line and add some text. The string I am looking for may or not exist in the file. If it can't find the string I want it to still close and save the file go on to the next file. The problem is that once it finds the last string and the message box cannot find wait 1 sec comes up. The window text shows when you use AU3info is: Cannot find "wait 1 sec" I have tried it with and without the double quotes script continues to run still. Here is the script that I have. Any help would be greatly appreciated. Thank you. ; Shows the filenames of all files in the current directory $search = FileFindFirstFile("F:\MACROS\private\test\test\*.mac") $find = "wait 1 sec" $replace = "wait 5 msec " ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop run("notepad.exe F:\MACROS\private\test\test\" & $file) do Sleep (500) send ("{CTRLDOWN}f{CTRLUP}") WinWait("Find","Fi&nd what:") sleep (500) Send($find) sleep (2500) ;this what the window text shows when you use AU3info ;Cannot find "wait 1 sec" ;I have tried it with and without the double quotes script continues to run If winexists ("Notepad","Cannot """"find wait 1 """) Then sleep (2500) Send("{SPACE}") sleep (2500) Send("{ESC}") sleep (2500) Send("{CTRLDOWN}s{CTRLUP}{ALTDOWN}{F4}{ALTUP}") EndIf send ("{enter}") sleep (500) Send("{ESC}") sleep (500) Send("{END}{ENTER}") send ($replace) until WinExists("Notepad","") MsgBox(0, "Error", "No files/directories matched the search pattern") WEnd Link to comment Share on other sites More sharing options...
Lazycat Posted August 15, 2005 Share Posted August 15, 2005 I see additional space before second quote and one unnecessary "" pair. Maybe this cause?If winexists ("Notepad", 'Cannot "find wait 1"') ThenAnyway, isn't simpler and safer to load macro into string with FileRead and search with StringInStr? Koda homepage ([s]Outdated Koda homepage[/s]) (Bug Tracker)My Autoit script page ([s]Outdated mirror[/s]) Link to comment Share on other sites More sharing options...
caspermurphy Posted August 15, 2005 Author Share Posted August 15, 2005 I see additional space before second quote and one unnecessary "" pair. Maybe this cause?If winexists ("Notepad", 'Cannot "find wait 1"') ThenAnyway, isn't simpler and safer to load macro into string with FileRead and search with StringInStr?<{POST_SNAPBACK}>Hi Lazycat,Thank you for your reply, however it did not work either. As for your suggestion, it might be safer to do a fileread and do a search. However, I am not that knowledgable in writing scripts and I was happy to get where I am. I will give it a try though. Thank you again for your help. Link to comment Share on other sites More sharing options...
/dev/null Posted August 15, 2005 Share Posted August 15, 2005 (edited) As for your suggestion, it might be safer to do a fileread and do a search. However, I am not that knowledgable in writing scripts and I was happy to get where I am. I will give it a try though.you might want to try this :-)expandcollapse popup#include <File.au3> $find = "wait 1 sec" $replace = "wait 5 msec" $filepath = "F:\MACROS\private\test\test\" $filepattern = "*.mac" $search = FileFindFirstFile($filepath & $filepattern) ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) if @error then exitloop $retval = ReplaceStringInFile($filepath & $file,$find,$replace,0,1) if $retval = -1 then msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $file) exit else msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &$find & " in the file: " & $file) endif wend func ReplaceStringInFile($filename, $searchstring, $replacestring,$caseness = 0, $occurance = 0) ; $filename = name of the file to open. ! Need the FULL path, not just the name returned by FileFindNextFile ! ; $searchstring = string to search ; $replacestring = string to replace ; $caseness = should case matter? 0 = NO (default), 1 = YES ; $occurence = shall we find all strings in the file or just the first? 0 = first only (default), 1 = ALL strings local $retval = 0 local $tempfile, $readhandle, $writehandle $tempfile = _TempFile() $readhandle = FileOpen($filename,0) if $readhandle = -1 then return -1 $writehandle = FileOpen($tempfile,2) if $writehandle = -1 then return -1 while 1 $line = FileReadLine($readhandle) if @error then exitloop if StringInStr($line,$searchstring,$caseness,$occurance) then $retval += 1 $line = StringReplace($line,$searchstring,$replacestring,0,$caseness) FileWriteLine($writehandle,$line) if $occurance = 0 then $retval = 1 exitloop endif else FileWriteLine($writehandle,$line) endif wend FileClose($readhandle) FileClose($writehandle) FileDelete($filename) FileMove($tempfile,$filename) return $retval endfuncCheersKurt Edited August 15, 2005 by /dev/null __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
caspermurphy Posted August 15, 2005 Author Share Posted August 15, 2005 you might want to try this :-)expandcollapse popup#include <File.au3> $find = "wait 1 sec" $replace = "wait 5 msec" $filepath = "F:\MACROS\private\test\test\" $filepattern = "*.mac" $search = FileFindFirstFile($filepath & $filepattern) ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) if @error then exitloop $retval = ReplaceStringInFile($filepath & $file,$find,$replace,0,1) if $retval = -1 then msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $file) exit else msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &$find & " in the file: " & $file) endif wend func ReplaceStringInFile($filename, $searchstring, $replacestring,$caseness = 0, $occurance = 0) ; $filename = name of the file to open. ! Need the FULL path, not just the name returned by FileFindNextFile ! ; $searchstring = string to search ; $replacestring = string to replace ; $caseness = should case matter? 0 = NO (default), 1 = YES ; $occurence = shall we find all strings in the file or just the first? 0 = first only (default), 1 = ALL strings local $retval = 0 local $tempfile, $readhandle, $writehandle $tempfile = _TempFile() $readhandle = FileOpen($filename,0) if $readhandle = -1 then return -1 $writehandle = FileOpen($tempfile,2) if $writehandle = -1 then return -1 while 1 $line = FileReadLine($readhandle) if @error then exitloop if StringInStr($line,$searchstring,$caseness,$occurance) then $retval += 1 $line = StringReplace($line,$searchstring,$replacestring,0,$caseness) FileWriteLine($writehandle,$line) if $occurance = 0 then $retval = 1 exitloop endif else FileWriteLine($writehandle,$line) endif wend FileClose($readhandle) FileClose($writehandle) FileDelete($filename) FileMove($tempfile,$filename) return $retval endfuncCheersKurt<{POST_SNAPBACK}>Kurt thank you for your reply.That worked great. I must not have been clear on what I would like to do. I do not want to replace the "wait 1 sec". I want to find that line then goto the next line and write "wait 5 msec", until end of file and then save the file. The code you were so kind to give me replaces wait 1 sec with wait 5 msec.Thank you I appreciate it your time in trying to assist me.John Link to comment Share on other sites More sharing options...
/dev/null Posted August 15, 2005 Share Posted August 15, 2005 I must not have been clear on what I would like to do. I do not want to replace the "wait 1 sec". I want to find that line then goto the next line and write "wait 5 msec", until end of file and then save the file. can you post an example? Before and after?CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
caspermurphy Posted August 15, 2005 Author Share Posted August 15, 2005 (edited) can you post an example? Before and after?CheersKurt<{POST_SNAPBACK}>Sure Kurt,Here is what a file might look like before and after.Now I have no idea why I just can't change the wait 1 sec to wait 5 sec. The macros just will not work. However if I add the 5msec to the next line they work just fine. I have over 200 macros that I need to change, I thought this would be easier then to have edit each file manually.05 b.mac is before change05 a.mac is after changeThank you again for your help.Johnoops I didn't get the fils to attach. hereis some code: before[wait inp inh]wait 1 sec until FieldAttribute 0000 at (9,76)[enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (4,62)[enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (7,1)"1[enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (7,1)[enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (4,36)[enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (9,16)and after :wait 1 sec until FieldAttribute 0000 at (9,76)wait 5 msec [enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (4,62)wait 5 msec [enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (7,1)wait 5 msec "1[enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (7,1)wait 5 msec [enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (4,36)wait 5 msec [enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (9,16)wait 5 msec I am sorry that I wasn't able to attach the files.John Edited August 15, 2005 by caspermurphy Link to comment Share on other sites More sharing options...
/dev/null Posted August 15, 2005 Share Posted August 15, 2005 and after :wait 1 sec until FieldAttribute 0000 at (9,76)wait 5 msec [enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (4,62)wait 5 msecO.K. this is not exactly what you described in your previous post: I do not want to replace the "wait 1 sec". I want to find that line then goto the next line and write "wait 5 msec", until end of file and then save the file.I'm a bit consused now! Can you describe a bit more what you want to achieve? Is it like this? File before:line 1: kllfkjfsdljfline 2: cbnmbxline 3: do somethin "wait 1 sec"line 4: wquiz239line 5: 8012830line 6: mnasdllkFile after:line 1: kllfkjfsdljfline 2: cbnmbxline 3: do somethin "wait 1 sec"line 4: wait 5 msecline 5: wait 5 msecline 6: wait 5 msecThis is at least what I thought when I read your reply to my first post !??!CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
/dev/null Posted August 15, 2005 Share Posted August 15, 2005 and after :wait 1 sec until FieldAttribute 0000 at (9,76)wait 5 msec [enter][wait inp inh]wait 1 sec until FieldAttribute 0000 at (4,62)wait 5 msec [enter]after I read your post the second time, I understood it.. There is only a minor change necessary in the code (marked with <==)expandcollapse popup#include <File.au3> $find = "wait 1 sec" $replace = "wait 5 msec" $filepath = "F:\MACROS\private\test\test\" $filepattern = "*.mac" $search = FileFindFirstFile($filepath & $filepattern) ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) if @error then exitloop $retval = ReplaceStringInFile($filepath & $file,$find,$replace,0,1) if $retval = -1 then msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $file) exit else msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &$find & " in the file: " & $file) endif wend func ReplaceStringInFile($filename, $searchstring, $replacestring,$caseness = 0, $occurance = 0) ; $filename = name of the file to open. ! Need the FULL path, not just the name returned by FileFindNextFile ! ; $searchstring = string to search ; $replacestring = string to replace ; $caseness = should case matter? 0 = NO (default), 1 = YES ; $occurence = shall we find all strings in the file or just the first? 0 = first only (default), 1 = ALL strings local $retval = 0 local $tempfile, $readhandle, $writehandle $tempfile = _TempFile() $readhandle = FileOpen($filename,0) if $readhandle = -1 then return -1 $writehandle = FileOpen($tempfile,2) if $writehandle = -1 then return -1 while 1 $line = FileReadLine($readhandle) if @error then exitloop if StringInStr($line,$searchstring,$caseness,$occurance) then $retval += 1 ; <<== removed a line here FileWriteLine($writehandle,$line) FileWriteLine($writehandle,$replacestring & @CRLF); <== added this ! if $occurance = 0 then $retval = 1 exitloop endif else FileWriteLine($writehandle,$line) endif wend FileClose($readhandle) FileClose($writehandle) FileDelete($filename) FileMove($tempfile,$filename) return $retval endfuncCheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
caspermurphy Posted August 16, 2005 Author Share Posted August 16, 2005 Good morning Kurt, thank you for helping me out. What was the reason for this line? $retval += 1 When I ran it came up with an error (49) : ==> Expected a "=" operator in assignment statement.: I just took out the + sign and worked great for me. Thank you again for your help. John Link to comment Share on other sites More sharing options...
buzz44 Posted August 16, 2005 Share Posted August 16, 2005 (edited) += aswell as -=, *=, /= and &= are the new operators included in the beta. They are used to simplify code such as $A = $A + 1 to just $A += 1, this also applies for multiplication, subtraction, division, and concatenation. Edited August 16, 2005 by Burrup qq Link to comment Share on other sites More sharing options...
seandisanti Posted August 16, 2005 Share Posted August 16, 2005 += aswell as -=, *=, /= and &= are the new operators included in the beta. They are used to simplify code such as $A = $A + 1 to just $A += 1, this also applies for multiplication, subtraction, division, and concatenation.<{POST_SNAPBACK}>are there ++ and -- yet? Link to comment Share on other sites More sharing options...
caspermurphy Posted August 16, 2005 Author Share Posted August 16, 2005 Burrup, thank you for the info. Does sound like it will make it easier. John Link to comment Share on other sites More sharing options...
/dev/null Posted August 16, 2005 Share Posted August 16, 2005 thank you for helping me out. What was the reason for this line? $retval += 1Hi John,Burrup already explained +=. The reason I use the statement $retval += 1, is that I want to return the number of occurrences of the search string found in the file.CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
caspermurphy Posted August 16, 2005 Author Share Posted August 16, 2005 Kurt, thank you and thank you for your help. And to everyone else that helped. John 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