Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect corrupt files

Status
Not open for further replies.

RaKKeR

Programmer
Nov 22, 2004
26
NL
Dear programmers,

I would like to know if and how I can detect when a file is corrupt (e.g. power down while file was being copied). I thought the function CopyFile() would fail in that case, but that was not the case. I did the following test: I took a very big file and copied it to a specific location. While copying I unplugged the power switch hoping the file would be corrupt. Unfortunately CopyFile() just copies the incorrect file without any problems...

Thx in advance,

RaKKeR
 
The only thing I can think of is to embed some validation inside the file (like a checksum and a length) which the application using the file can then check. ZIP files for instance take this approach.

Transaction logging only works (as far as I know) at the file system level, to basically ensure that you always have a valid file system.

If power is lost during a file copy, NTFS will ensure that the file system is in a valid state, but all you will see is either no file at all, or a truncated file.

--
 
Before you copy the file, create a log file and output something like "Starting to copy file x from a to b.". Close that file.
Now copy the file.
Then write to the log file again "Finished copying file x from a to b."

If you have a "Starting" without a "Finished" log entry, the file wasn't copied properly, so delete it if it exists and try again.
If you have no "Starting" or "Finished" log entry, an error happened even before you could write to the log. Try copying again.
If you have both "Starting" and "Finished" log entries, the file was successfully copied.

You can also include CRC checksums in the log file and compare after copying is complete if you're worried about corruption other than power loss...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top