Jump to content
Sign in to follow this  
m1975michael

Replace Text in File

Recommended Posts

m1975michael

#include <File.au3>

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    For $i = 1 To _FileCountLines($sDirFile)
        Local $sDir = FileReadLine($sDirFile, $i)
        StringRegExpReplace($sDir, "(<tr style=.+)", "")
        FileWrite($sDirFile, $sDir)
    Next
EndFunc   ;==>DirectoryCleanUp

Hello,

I need to replace text in a file. I know the RegEx expression is correct and it will replace the text in a simple string but I can not seem to remove the text from the file.  I do not want create a new file just modify the one I am working with.  Any assistance would be appreciated.

Michael

Share this post


Link to post
Share on other sites
mikell

The For loop is a heavy approach

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    $sDir = FileRead($sDirFile)
    $res = StringRegExpReplace($sDir, "(<tr style=\V+)", "")
    $file = FileOpen($sDirFile, 2)
    FileWrite($file, $res)
    FileClose($file)
EndFunc
Edited by mikell

Share this post


Link to post
Share on other sites
m1975michael

I have looked in the forums but I have not seen any examples that use the StringRegExpReplace.  Sorry I am new to scripting.

Search the example forums, this has been done before.

Share this post


Link to post
Share on other sites
m1975michael

 

The For loop is a heavy approach

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    $sDir = FileRead($sDirFile)
    $res = StringRegExpReplace($sDir, "(<tr style=\V+)", "")
    $file = FileOpen($sDirFile, 2)
    FileWrite($file, $res)
    FileClose($file)
EndFunc

Thank you mikell for your assistance and simpler solution.  I am still learning. :)

Share this post


Link to post
Share on other sites
m1975michael

Hello mikell,

If I have several item to replace in the text file how should I accomplish this?  I added another string to replace with a new variable and a nother FileWrite function but only the first text item is replaced.

Edited by m1975michael

Share this post


Link to post
Share on other sites
Starstar

_ReplaceStringInFile($filename, $find, $replace)

try this.......


Life is like a coin. You can spend it Anyway as you wish and for your kind information. "you can spend it only once."

Share this post


Link to post
Share on other sites
m1975michael
_ReplaceStringInFile($filename, $find, $replace)

try this.......

I am not sure how I can use the StringRegExpReplace function with that function.

Share this post


Link to post
Share on other sites
Danyfirex

Hi. You're no updating your line.

Look:

#include <File.au3>

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    Local $sDir=""
    For $i = 1 To _FileCountLines($sDirFile)
        $sDir = FileReadLine($sDirFile, $i)
        $sDir=StringRegExpReplace($sDir, "(<tr style=.+)", "")
        FileWrite($sDirFile, $sDir)
    Next
EndFunc   ;==>DirectoryCleanUp

I think something so, should work 

#include <File.au3>

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    local $hFile=0
    Local $sData=Fileread($sDirFile)
    $sData=StringRegExpReplace($sData,"(<tr style=.+)", "")
    $hFile=FileOpen($sDirFile,2)
    FileWrite($hFile, $sData)
    FileClose($hFile)
EndFunc   ;==>DirectoryCleanUp

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites
m1975michael

Hi. You're no updating your line.

Look:

#include <File.au3>

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    Local $sDir=""
    For $i = 1 To _FileCountLines($sDirFile)
        $sDir = FileReadLine($sDirFile, $i)
        $sDir=StringRegExpReplace($sDir, "(<tr style=.+)", "")
        FileWrite($sDirFile, $sDir)
    Next
EndFunc   ;==>DirectoryCleanUp

I think something so, should work 

#include <File.au3>

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    local $hFile=0
    Local $sData=Fileread($sDirFile)
    $sData=StringRegExpReplace($sData,"(<tr style=.+)", "")
    $hFile=FileOpen($sDirFile,2)
    FileWrite($hFile, $sData)
    FileClose($hFile)
EndFunc   ;==>DirectoryCleanUp

Saludos

This does work but how can I do multiple replacements without opening and rewriting the file?  Thank you for your assistance.

Edited by m1975michael

Share this post


Link to post
Share on other sites
mikell

Here is the simplest approach to do several replaces

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    $sDir = FileRead($sDirFile)
    $res = StringRegExpReplace($sDir, "(<tr style=\V+)", "")   ; first replace

    $res = StringRegExpReplace($res, "(pattern_2)", "")   ; 2nd replace

    $res = StringRegExpReplace($res, "(pattern_3)", "")   ; 3rd replace

    $file = FileOpen($sDirFile, 2)
    FileWrite($file, $res)
    FileClose($file)
EndFunc

Of course you can also use a single regex

$res = StringRegExpReplace($sDir, "(<tr style=\V+)|(pattern_2)|(pattern_3)", "")
Edited by mikell

Share this post


Link to post
Share on other sites
Danyfirex

Change the output file name. 

 

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites
m1975michael

 

Here is the simplest approach to do several replaces

DirectoryCleanUp()

Func DirectoryCleanUp()
    Local $sDirFile = "C:\Users\mender\Downloads\Scripts\AutoIT\CEDAddress\CED_Location_DirectoryOrg.htm"
    $sDir = FileRead($sDirFile)
    $res = StringRegExpReplace($sDir, "(<tr style=\V+)", "")   ; first replace

    $res = StringRegExpReplace($res, "(pattern_2)", "")   ; 2nd replace

    $res = StringRegExpReplace($res, "(pattern_3)", "")   ; 3rd replace

    $file = FileOpen($sDirFile, 2)
    FileWrite($file, $res)
    FileClose($file)
EndFunc

Of course you can also use a single regex

$res = StringRegExpReplace($sDir, "(<tr style=\V+)|(pattern_2)|(pattern_3)", "")

Thank you again mikell.   I didn't realize you could replace more than one pattern using one StringRegExpReplace statement.  I am just starting to lean RegEx too. :)

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  

×