Jump to content

Replace Text in File


Recommended Posts

#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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Change the output file name. 

 

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

 

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

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