Home All Groups Group Topic Archive Search About

How to "Archive" a text file with multiple handles open

Author
16 Jun 2006 5:06 PM
dwainew
Problem: I'd like to archive off the contents of a text file.  However,
other processes may have the file open, so MoveTo(newfilepath) can't be
done, as far as I've found.

Start with a 5 MB MyLog.txt file that may be opened (reading and/or
writing) by several handles.  The Final state I'm looking for is an
archive file MyLog_YYYYMMDDHHMMSS.txt with the 5 MB contents and the
original file zeroed out or with a  line indicating the archive
operation.

I'm looking for a way to use CopyTo(NewFilePath) and then "zero out"
the original file....

problem is, I've found no real discussions regarding DELETING text in a
previously established file.  How can you use a binarywriter or similar
class to REMOVE bytes or add null bytes.  I end up with a file of the
original length.

you can't use the traditional FileInfo instantiation with the Truncate
parameter, as this causes an access violation.

Any ideas?

Author
16 Jun 2006 5:56 PM
GhostInAK
Hello dwai***@gmail.com,

The design is a little flawed.  My first question is.. do you own all the
file handles?  If so you may wish to create a single point of writing/reading
which could then easily have the archive code added to it.  I use a similar
method for writing to multiple log locations.

While there are open file handles you can't "zero out" (erase the contents
of) a file.  The reason being that the other file streams are looking for
data at a particular location.. if you erase the file.. these other streams
have no way of knowing this and can not reset the location they are looking
to read/write.

-Boo

Show quoteHide quote
> Problem: I'd like to archive off the contents of a text file.
> However, other processes may have the file open, so
> MoveTo(newfilepath) can't be done, as far as I've found.
>
> Start with a 5 MB MyLog.txt file that may be opened (reading and/or
> writing) by several handles.  The Final state I'm looking for is an
> archive file MyLog_YYYYMMDDHHMMSS.txt with the 5 MB contents and the
> original file zeroed out or with a  line indicating the archive
> operation.
>
> I'm looking for a way to use CopyTo(NewFilePath) and then "zero out"
> the original file....
>
> problem is, I've found no real discussions regarding DELETING text in
> a previously established file.  How can you use a binarywriter or
> similar class to REMOVE bytes or add null bytes.  I end up with a file
> of the original length.
>
> you can't use the traditional FileInfo instantiation with the Truncate
> parameter, as this causes an access violation.
>
> Any ideas?
>