Jump to content

Recommended Posts

Posted

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.

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!

Posted

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!

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...