DCLXVI Posted March 24, 2010 Posted March 24, 2010 Consider this simplistic snippet to replace text in a file. $FileDat = FileRead("file.txt") $FileDat = StringReplace($FileDat,"cat","dog") FileDelete("file.txt") FileWrite("file.txt",$FileDat) How can I replace text in a file WITHOUT deleting the file and replacing it with a new one? In other words, I need a more complicated way to achieve the same functionality as above. Nota Bene: I am using scripts to generate network file server traffic for analysis. Therefore I need examples of file modifications (not delete and write, such as above example accomplishes). Thank you much.
Fulano Posted March 24, 2010 Posted March 24, 2010 (edited) I don't believe that it can be done in AutoIt, the only other way I can think to do it would be:1: open the file2: read it into a var3: close the file4: reopen it for writing5: write the changed var to the file6: close the fileOnly problem is that does almost exactly what you are doing, it just hides it behind the implementation (or rather it zeros the file rather than deleting it, but it's a pretty much moot point in terms of performance IMHO).If you want, there is a UDF function in File.au3 that does it (_ReplaceStringInFile) but all it does is what I've outlined.Hope this helps Edited March 24, 2010 by Fulano #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
GodlessSinner Posted March 24, 2010 Posted March 24, 2010 Look at _WinAPI_WriteFile in Help.chm, there explains how to write bytes, using FilePointer.. _____________________________________________________________________________
Fulano Posted March 24, 2010 Posted March 24, 2010 Sneaky I'll have to remember that. #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
MvGulik Posted March 24, 2010 Posted March 24, 2010 (edited) whatever Edited February 7, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
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