Sign in to follow this  
Followers 0
DCLXVI

File modification

5 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 file

2: read it into a var

3: close the file

4: reopen it for writing

5: write the changed var to the file

6: close the file

Only 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 Posted Image

Edited 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!

Share this post


Link to post
Share on other sites

Look at _WinAPI_WriteFile in Help.chm, there explains how to write bytes, using FilePointer..


_____________________________________________________________________________

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

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  
Followers 0