Sign in to follow this  
Followers 0
jorgeng

replace string from file

7 posts in this topic

#1 ·  Posted (edited)

I want to replace a word in a string read from file but can't get it to work, can someone see what's wrong? $trade_original is empty after replace...

#Include <File.au3>

        $file_Omx_Wampa_PHP_Mangold = FileOpen("C:\Filer\Rankor System\Omx_Wampa_Trades_PHP_Mangold.txt", 0)

;       Check if file opened for reading OK
        If $file_Omx_Wampa_PHP_Mangold = -1 Then
            MsgBox(0, "Error", "Unable to open file Omx_Wampa_PHP_Mangold.")
            Exit
        EndIf

;       Read in lines of text until the EOF is reached
        While 1
            $sData = FileReadLine($file_Omx_Wampa_PHP_Mangold)
            If @error = -1 Then ExitLoop
            MsgBox(0, "Line read:", $sData)
        Wend

;       $sData = '10:16 ORDER "sl) Omx Wampa - Entry Short - Kl.10 - Röd - Okrypterat OMXS300I" kurs 1050.0000$'
        $trade_original = StringReplace($sData, "Entry", "Exit")

        ConsoleWrite("1=" & $trade_original & @CRLF)
Edited by jorgeng

Share this post


Link to post
Share on other sites



hey, I need the answer for that too. Actually I want to modify some strings in a very big file size, directly in the memory. I don't want to use heavy method like FileWrite and overwrite.

Share this post


Link to post
Share on other sites

Try _ReplaceStringInFile function Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

hey, I need the answer for that too. Actually I want to modify some strings in a very big file size, directly in the memory. I don't want to use heavy method like FileWrite and overwrite.

I just want a simple replace of string since i shall delete file and do filewrite with new content...

It seems that it has to be simple text in

StringReplace($sData

not accepting $sData

Edited by jorgeng

Share this post


Link to post
Share on other sites

I just want a simple replace of string since i shall delete file and do filewrite with new content...

It seems that it has to be simple text in

StringReplace($sData

not accepting $sData

I repeat :

try _ReplaceStringInFile function !


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I want to replace a word in a string read from file but can't get it to work, can someone see what's wrong? $trade_original is empty after replace...

;       Read in lines of text until the EOF is reached
        While 1
            $sData = FileReadLine($file_Omx_Wampa_PHP_Mangold)
            If @error = -1 Then ExitLoop
            MsgBox(0, "Line read:", $sData)
        Wend

;       $sData = '10:16 ORDER "sl) Omx Wampa - Entry Short - Kl.10 - Röd - Okrypterat OMXS300I" kurs 1050.0000$'
        $trade_original = StringReplace($sData, "Entry", "Exit")

        ConsoleWrite("1=" & $trade_original & @CRLF)

The way you're reading your file in means that the only thing that $sData is going to contain is the last line of the file and nothing else. If you want $sData to contain the whole file's contents then you need to either write it like this:

While 1
            $sData &= FileReadLine($file_Omx_Wampa_PHP_Mangold) ;<===Notice the "&" before the "="
            If @error = -1 Then ExitLoop
            MsgBox(0, "Line read:", $sData)
Wend

Or like this without the While..Wend:

$sData = FileRead($file_Omx_Wampa_PHP_Mangold)
        MsgBox(0, "Line read:", $sData)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Try _ReplaceStringInFile function Posted Image

Thx. It only allow text editing I guess. Is there any way to binary edit it or hex edit it?

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