Jump to content

Replace a string in a file


Recommended Posts

Hello

I want to change one string in a script with another script, but I don't know part of the string (French sentences, but it's not important)

$fichier = FileOpen("taille.au3",0)

If ($fichier = -1) Then
    MsgBox ( 0, "Erreur", "Impossible de trouver le fichier de script")
Else
    $lecture = FileRead($fichier, 1000)
    $error = StringReplace ($lecture,"$max = 100", "$max = 200")
              ; I want to put a joker or same kind of character instead of "100"
              ; because I don't know what number it will be...

    MsgBox( 0, "test",$lecture & "\n")
    FileClose($fichier)
    $fichier = FileOpen("taille.au3",2)
    FileWrite ($fichier,$lecture)
    FileClose($fichier)
    
EndIf

The MsgBox() is there to test the value

Edited by 440LVB
Link to comment
Share on other sites

If you do not know what number will be included, use StringRegExpReplace in the beta version. Will the file always be smaller than 1000 bytes? If not, you should probably do the following:

Read the entire file into a string or array.

Use StringRegExpReplace to make the approprate replacements.

Write the file.

$fichier = FileOpen("taille.au3",0)

If ($fichier = -1) Then
    MsgBox ( 0, "Erreur", "Impossible de trouver le fichier de script")
Else
   ; Read entire file, not just first 1000 characters.
    $lecture = ""
    Do
         $lecture = $lecture & FileRead($fichier, 1000)
    Until @Error = -1

   ; Perform the replacement.
    $lecture = StringRegExpReplace($lecture, "$max = \d+", "$max = 200", 0)

    MsgBox( 0, "test",$lecture & "\n")
    FileClose($fichier)
    $fichier = FileOpen("taille.au3",2)
    FileWrite ($fichier,$lecture)
    FileClose($fichier)
EndIf

Edit: Fix speeling errors

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

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