James Posted February 16, 2008 Share Posted February 16, 2008 Hi,Something I whipped up for, this! Quite fast for 100 lines average is about 64 milliseconds on a 994MHz Someone probably has something better and simpler but it's good to learn!#include <File.au3> ReplaceInFile(@ScriptDir & '\test.txt', "hi", "howdy") Func ReplaceInFile($io_file, $io_word, $io_replacement) Dim $Records $Lines = _FileCountLines($io_File) If Not _FileReadToArray($io_file, $Records) Then ConsoleWriteError("There was an error reading > " & $io_file & @CRLF) Exit ElseIf _FileReadToArray($io_file, $Records) == "" Then ConsoleWriteError("File was found to be blank!" & @CRLF) ElseIf Not @error Then ConsoleWrite("> File was all good!" & @CRLF) EndIf $File = FileOpen($io_file, 2) ; DEBUG = ConsoleWrite("+> Found: " & $Lines & " lines!" & @CRLF) For $ax = 1 To $Records[0] ConsoleWrite("+> " & $Records[$ax] & @CRLF) $Replace = StringReplace($Records[$ax], $io_word, $io_replacement) ConsoleWrite("+> " & $Replace & @CRLF) ;For $ay = $Lines To UBound($Lines) - 1 ; FileWrite($File, $Replace) ;Next FileWrite($File, $Replace & @CRLF) If Not @error Then ConsoleWrite("> Replaced String!" & @CRLF) Else ConsoleWriteError("Error replacing text!" & @CRLF) EndIf Next FileClose($File) EndFuncIn the ReplaceInFile(), replace the filename and the strings to replace.Thanks,James Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
JustinReno Posted February 16, 2008 Share Posted February 16, 2008 _ReplaceStringInFile() Link to comment Share on other sites More sharing options...
James Posted February 16, 2008 Author Share Posted February 16, 2008 Damn knew there would be something. Mine still works! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
JustinReno Posted February 16, 2008 Share Posted February 16, 2008 I tried to make a simpler version and I typed in the name and found out it was a function! Link to comment Share on other sites More sharing options...
James Posted February 16, 2008 Author Share Posted February 16, 2008 Lol. Didn't see it! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
JustinReno Posted February 16, 2008 Share Posted February 16, 2008 Yeah, I didn't even notice that function until the name I typed turned blue. :) Link to comment Share on other sites More sharing options...
James Posted February 16, 2008 Author Share Posted February 16, 2008 For your own function? Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
JustinReno Posted February 16, 2008 Share Posted February 16, 2008 Yeah, I was trying to make a simpler one. Global $File = @ScriptDir&"\Replace.txt" Global $String = "Hello," Global $NewString = "Hi," FileWrite($File, "Hello, my name is Justin.") _StringReplaceInFile($File, $String, $NewString) Func _StringReplaceInFile($H_File, $S_String, $S_NewString) $S_ReadFile = FileRead($H_File) FileDelete($H_File) $GetEachWord = StringSplit($S_ReadFile, " ") For $I = 1 To $GetEachWord[0] If $GetEachWord[$I] = $S_String Then FileWrite($H_File, StringReplace($S_ReadFile, $S_String, $S_NewString)) Next EndFunc Link to comment Share on other sites More sharing options...
James Posted February 16, 2008 Author Share Posted February 16, 2008 Oh cool! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
JustinReno Posted February 16, 2008 Share Posted February 16, 2008 Thanks! 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