Sign in to follow this  
Followers 0
mdwerne

Replace bit of text in file and close...

5 posts in this topic

Hello,

I have been reading through the forums and the help and have gotten this far...yes, it's not very. :-(

What I'm trying to do, using the most efficient method is:

1) Open a small file

2) Search for some specific text

3) Replace the text with other text

4) Save and close the file

Here is what I have so far (not working).

$file = FileOpen("Settings.LiveUpdate")
$read = FileRead($file)
If @error = -1 Then
    MsgBox(0, "Error", "File not read")
    Exit
Else
    If StringRegExp($read, "PROXY=USE_IE_SETTINGS") Then
        StringRegExpReplace($read, "PROXY=USE_IE_SETTINGS", "PROXY=NOT_USED")
    Else
        MsgBox(0, "Error", "Text not found")
    EndIf
EndIf
FileClose($file)

Any suggestions or other methods I should look at?

Thanks, Mike

Share this post


Link to post
Share on other sites



1. After reading the file, close it immediately.

2. Use StringReplace() vice StringRegExpReplace(), and save the result to a variable (i.e. $write).

3. Reopen the file (or a new one) in write mode with FileOpen().

4. FileWrite($file, $write)

5. Close the file again.

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You can make it even simpler since you can read what's open for writing.

$sToFind = "Some String"
$sToReplaceWith = "Another String"

; Step 1
$hFile = FileOpen(@ScriptFullPath, 1)
FileSetPos($hFile, 0, 0)
; Step 2
$sString = FileRead($hFile)
$sString = StringReplace($sString, $sToFind, $sToReplaceWith)
; Step 3
FileSetPos($hFile, 0, 0)
FileWrite($hFile, $sString)
; Step 4
FileClose($hFile)
Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

1. After reading the file, close it immediately.

2. Use StringReplace() vice StringRegExpReplace(), and save the result to a variable (i.e. $write).

3. Reopen the file (or a new one) in write mode with FileOpen().

4. FileWrite($file, $write)

5. Close the file again.

:idea:

Thanks PsaltyDS...a push in the right direction was all I needed. Here is what I have now (quick and dirty version)

$sMyFile = FileOpen("Settings.LiveUpdate", 0)
$sReadMyFile = FileRead($sMyFile)
FileClose($sMyFile)
$sResult = StringReplace($sReadMyFile, "PROXY=USE_IE_SETTINGS", "PROXY=NOT_USED")
$file = FileOpen("Settings.LiveUpdate", 2)
FileWrite($file, $sResult)
FileClose($file)

Seems to work!!! :)

Share this post


Link to post
Share on other sites

You can make it even simpler since you can read what's open for writing.

$sToFind = "Some String"
$sToReplaceWith = "Another String"

; Step 1
$hFile = FileOpen(@ScriptFullPath, 1)
FileSetPos($hFile, 0, 0)
; Step 2
$sString = FileRead($hFile)
$sString = StringReplace($sString, $sToFind, $sToReplaceWith)
; Step 3
FileSetPos($hFile, 0, 0)
FileWrite($hFile, $sString)
; Step 4
FileClose($hFile)

Thanks trancexx for the alternate method! It's nice seeing how others approach the same task. Now if I could just remember what I've learned today! :idea:

Share this post


Link to post
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
Sign in to follow this  
Followers 0