m1975michael Posted February 20, 2014 Share Posted February 20, 2014 #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 More sharing options...
DicatoroftheUSA Posted February 20, 2014 Share Posted February 20, 2014 Search the example forums, this has been done before. Statism is violence, Taxation is theft. Autoit Wiki Link to comment Share on other sites More sharing options...
mikell Posted February 20, 2014 Share Posted February 20, 2014 (edited) 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 February 20, 2014 by mikell Link to comment Share on other sites More sharing options...
m1975michael Posted February 20, 2014 Author Share Posted February 20, 2014 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. Link to comment Share on other sites More sharing options...
m1975michael Posted February 20, 2014 Author Share Posted February 20, 2014 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 More sharing options...
m1975michael Posted February 20, 2014 Author Share Posted February 20, 2014 (edited) 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 February 20, 2014 by m1975michael Link to comment Share on other sites More sharing options...
Starstar Posted February 20, 2014 Share Posted February 20, 2014 _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." Link to comment Share on other sites More sharing options...
m1975michael Posted February 20, 2014 Author Share Posted February 20, 2014 _ReplaceStringInFile($filename, $find, $replace) try this....... I am not sure how I can use the StringRegExpReplace function with that function. Link to comment Share on other sites More sharing options...
Danyfirex Posted February 20, 2014 Share Posted February 20, 2014 (edited) 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 February 20, 2014 by Danyfirex Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
m1975michael Posted February 20, 2014 Author Share Posted February 20, 2014 (edited) 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 February 20, 2014 by m1975michael Link to comment Share on other sites More sharing options...
mikell Posted February 20, 2014 Share Posted February 20, 2014 (edited) 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 February 20, 2014 by mikell Link to comment Share on other sites More sharing options...
Danyfirex Posted February 20, 2014 Share Posted February 20, 2014 (edited) Change the output file name. Saludos Edited February 20, 2014 by Danyfirex Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
m1975michael Posted February 20, 2014 Author Share Posted February 20, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now