czardas Posted July 18, 2007 Share Posted July 18, 2007 Hi. Can anyone help me solve this problem. I have modified an example from the help menu to replace text in a file. It works fine. and here it is: $replace = InputBox("REPLACE", "Text to replace") $replacement = InputBox("REPLACE WITH", "Text to insert") $file = FileOpen("test.aiml", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf #include <file.au3> Dim $aRecords If Not _FileReadToArray("test.aiml",$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf For $x = 1 to $aRecords[0] If StringInStr($aRecords[$x], $replace) Then $x1 = StringReplace ($aRecords[$x], $replace, $replacement, 1) _FileWriteToLine("test.aiml", $x, $x1 , 1) Else EndIf NextoÝ÷ ا"·©y«më®*m¶Þ¦VzØ^±©µìmv¬yÊ'vØbÚ+Þ~)^²Ø^±Êâ¦×âÛ-ë^ÆÖîµÚ²z-êeiÇ¢¶'°ØmëÚ®&í7ëOz¸Z¦Ëaz··öƧy«2Á¬¡Ú-+ºÒ7öl(׫x0+aÇhzÉ÷öÜ(®F®¶sbb33c·&WÆ6RÒçWD&÷gV÷Cµ$UÄ4RgV÷C²ÂgV÷CµFWBFòfæBgV÷C²¢b33c·&WÆ6VÖVçBÒçWD&÷gV÷Cµ$UÄ4RtDgV÷C²ÂgV÷CµFWBFòç6W'BgV÷C² £µ&WVBf÷"WfW'fÆP¢b33c¶fÆRÒfÆT÷VâgV÷C´GfW&'2æÖÂgV÷C²Â¤bb33c¶fÆRÒÓFVà¢×6t&÷ÂgV÷C´W'&÷"gV÷C²ÂgV÷CµVæ&ÆRFò÷VâfÆRâgV÷C²¢W@¤VæD` ¢6æ6ÇVFRfÇC¶fÆRæS2fwC°¤FÒb33c¶&V6÷&G0¤bæ÷BôfÆU&VEFô'&gV÷C´GfW&'2æÖÂgV÷C²Âb33c¶&V6÷&G2FVà¢×6t&÷CbÂgV÷C´W'&÷"gV÷C²ÂgV÷C²W'&÷"&VFærÆörFò'&W'&÷#¢gV÷C²fײW'&÷"¢W@¤VæD`¤f÷"b33c·ÒFòb33c¶&V6÷&G5³Ð b7G&ætå7G"b33c¶&V6÷&G5²b33c·ÒÂb33c·&WÆ6RFVà b33c·Ò7G&æu&WÆ6Rb33c¶&V6÷&G5²b33c·ÒÂb33c·&WÆ6RÂb33c·&WÆ6VÖVçB ôfÆUw&FUFôÆæRgV÷C´GfW&'2æÖÂgV÷C²Âb33c·Âb33c·Â VÇ6P VæD`¤æW@¤fÆT6Æ÷6RgV÷C´GfW&'2æÖÂgV÷C² £µ&WVBf÷"WfW'fÆP¢b33c¶fÆRÒfÆT÷VâgV÷C´æÖÂgV÷C²Â¤bb33c¶fÆRÒÓFVà¢×6t&÷ÂgV÷C´W'&÷"gV÷C²ÂgV÷CµVæ&ÆRFò÷VâfÆRâgV÷C²¢W@¤VæD`¢6æ6ÇVFRfÇC¶fÆRæS2fwC° ¤FÒb33c¶&V6÷&G0¤bæ÷BôfÆU&VEFô'&gV÷C´æÖÂgV÷C²Âb33c¶&V6÷&G2FVà¢×6t&÷CbÂgV÷C´W'&÷"gV÷C²ÂgV÷C²W'&÷"&VFærÆörFò'&W'&÷#¢gV÷C²fײW'&÷"¢W@¤VæD`¤f÷"b33c·ÒFòb33c¶&V6÷&G5³Ð b7G&ætå7G"b33c¶&V6÷&G5²b33c·ÒÂb33c·&WÆ6RFVà b33c·Ò7G&æu&WÆ6Rb33c¶&V6÷&G5²b33c·ÒÂb33c·&WÆ6RÂb33c·&WÆ6VÖVçB ôfÆUw&FUFôÆæRgV÷C´æÖÂgV÷C²Âb33c·Âb33c·Â VÇ6P VæD`¤æW@¤fÆT6Æ÷6RgV÷C´æÖÂgV÷C² £¶WF The script works until it reaches the line: _FileWriteToLine("AI.aiml", $x, $x1 , 1) I can't see why this doesn't work. There are about 50 files requiring multiple adjustments, so writing a separate script for each file is a pain. Where am I going wrong? operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Zedna Posted July 18, 2007 Share Posted July 18, 2007 (edited) $replace = InputBox("REPLACE", "Text to find") $replacement = InputBox("REPLACE WITH", "Text to insert") ;Repeat for every file _MyTextReplace("Adverbs.aiml") _MyTextReplace("AI.aiml") _MyTextReplace("...") ; ... Func _MyTextReplace($file_name) $file_handle = FileOpen($file_name,2) If @error Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $text = FileRead($file_handle) If @error Then MsgBox(0, "Error", "Unable to read file.") Exit EndIf $text = StringReplace ($text, $replace, $replacement) FileWrite($file_handle, $text) If @error Then MsgBox(0, "Error", "Unable to write file.") Exit EndIf FileClose($file_handle) EndFuncEDIT: added missing FileClose($file_handle) Edited July 19, 2007 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
czardas Posted July 18, 2007 Author Share Posted July 18, 2007 Thanks I'll try that. I still have to get my head around writing functions. This looks much neater than my script, and will be good for me to learn from. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted July 18, 2007 Author Share Posted July 18, 2007 Hmm unable to read file? Perhaps it will work if I change the file extensions. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted July 18, 2007 Author Share Posted July 18, 2007 Nope, it doesn't work. I'm flumuxed. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted July 18, 2007 Author Share Posted July 18, 2007 In Autoit a file can be opened either in read mode, or in write mode. Reading in write mode isn't supposed to work... I think Zedna had a little brain fart Right?! Perhaps I'm just a bit tired right now. I suppose you mean: open the file > read it > close it > replace text > close it again. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Siao Posted July 18, 2007 Share Posted July 18, 2007 Maybe try replacing that function with this: Func _MyTextReplace($file_name) $text = FileRead($file_name) If @error Then MsgBox(0, "Error", "Unable to read file.") Exit EndIf $file_handle = FileOpen($file_name,2) If @error Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $text = StringReplace ($text, $replace, $replacement) FileWrite($file_handle, $text) If @error Then MsgBox(0, "Error", "Unable to write file.") Exit EndIf EndFunc "be smart, drink your wine" Link to comment Share on other sites More sharing options...
czardas Posted July 18, 2007 Author Share Posted July 18, 2007 Maybe try replacing that function with this: Func _MyTextReplace($file_name) $text = FileRead($file_name) If @error Then MsgBox(0, "Error", "Unable to read file.") Exit EndIf $file_handle = FileOpen($file_name,2) If @error Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $text = StringReplace ($text, $replace, $replacement) FileWrite($file_handle, $text) If @error Then MsgBox(0, "Error", "Unable to write file.") Exit EndIf EndFunc Same result. Unable to read file. Can a file be read if it hasn't been opened? I think I need to look again in the morning, I'll be a bit fresher. Thanks for the input though. It could be something simple that I'm missing, and a good nights sleep might solve it. Otherwise I'll check back here again tomorrow. Cheers. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Zedna Posted July 19, 2007 Share Posted July 19, 2007 Hmm unable to read file? Perhaps it will work if I change the file extensions.I updated my original post:EDIT: added missing FileClose($file_handle) Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
czardas Posted July 19, 2007 Author Share Posted July 19, 2007 I updated my original post:EDIT: added missing FileClose($file_handle)I'm still getting the error message: unable to read file. Perhaps there is a good reason for this. Some of the files may contain well over 100,000 characters, though a few contain less than 1000. I don't know if there is an upper limit to reading a file. This was my reason for sorting each line into an array prior to attempting to replace all recurring instances of the selected text.In an array each line appears to be readable, but I think there must be something wrong with my code because the replacement does not seem to be working when there are 45 such files to read. I need to work on it a bit longer to try and find out where I'm going wrong, or am I trying to do the impossible? I have had a small success with my original script. It has worked for two small files, so I'll just add a few more to see if I hit a limit, or discover something else. Thanks for your imput, any further input will be very welcome. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Zedna Posted July 19, 2007 Share Posted July 19, 2007 Here is corrected script. Problem was in fact that file can't be opened in read and write mode at one time. $replace = InputBox("REPLACE", "Text to find") $replacement = InputBox("REPLACE WITH", "Text to insert") ;Repeat for every file _MyTextReplace("Adverbs.aiml") _MyTextReplace("AI.aiml") _MyTextReplace("...") ; ... Func _MyTextReplace($file_name) $file_handle = FileOpen($file_name,0) If @error Then MsgBox(0, "Error", "Unable to open file for read.") Exit EndIf $text = FileRead($file_handle) If @error Then MsgBox(0, "Error", "Unable to read file.") Exit EndIf FileClose($file_handle) $text = StringReplace ($text, $replace, $replacement) $file_handle = FileOpen($file_name,2) If @error Then MsgBox(0, "Error", "Unable to open file for write.") Exit EndIf FileWrite($file_handle, $text) If @error Then MsgBox(0, "Error", "Unable to write file.") Exit EndIf FileClose($file_handle) EndFunc Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
czardas Posted July 20, 2007 Author Share Posted July 20, 2007 That's done the trick. I've learned something. I have written a various functions, but have never declared them as such. Now I can incorporate functions and call on them when needed. operator64 ArrayWorkshop 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