Jump to content

Script missing something?


Recommended Posts

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?

Link to comment
Share on other sites

$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)
EndFunc

EDIT: added missing FileClose($file_handle)

Edited by Zedna
Link to comment
Share on other sites

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 :whistle:

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. :lmao:

Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

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.

:whistle:

Link to comment
Share on other sites

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
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...