Fog Creek Software
Discussion Board

Has a file changed?

I want to detect if a file has changed. I don't want to be notified, I just want to go back and look at it and see if the content has changed. I do not want to have to read the content. I'm happy that it could be circumventable. What I'm looking for is a 99% sure way of seeing if a file has changed.

Can I rely on Date/Time stamp alone? Should I combine with size for extra check?

Oh, this is on Windows BTW.

Tuesday, June 1, 2004

On Unix you'd use the 'stat' call and check for modified date or file size as your indicator.  Don't know what Win would use.

Tuesday, June 1, 2004

Clear the archive flag.

In the dos box:

C:\> attrib -a file

When you want to see if file has changed, look at the archive flag:

C:\> attrib file

If the line starts with an A, the archive flag is set, indicating that the file has changed since the archive flag was cleared.

René Nyffenegger
Tuesday, June 1, 2004

Trouble with relying on the archive bit is that some other program might set or unset it, like a backup program. I'd rely on a combination of file size and last modified.

Matthew Lock
Tuesday, June 1, 2004

If you have the .NET runtime installed, they have a class called FileSystemWatcher that can respond to change events within the file system

"Listens to the file system change notifications and raises events when a directory, or file in a directory, changes."

One of the Microsoft Press .NET books uses it as an example. It's not the most robust thing in the world, but sounds like it might be what you are looking for.

Tuesday, June 1, 2004

If you have .NET you can easily use an MD5 checksum to check if the file has changed.  This is what I do in my version control system.  Works like a champ.  Of course this requires writing a few lines of code.  Just thought I'd throw this at you.

Tuesday, June 1, 2004

Use the file date.  It will work 99% of the time.

You can make this a bit more reliable by assuming the file is different if the date is different (i.e. if the date is earlier or later than the reference date).

Tuesday, June 1, 2004

create a hash for each file in each folder and periodically rerun the hash to see if there is any diff.

Tuesday, June 1, 2004

Sound like date/time + size will work ok for me.

Other suggestions were good but did not meet the basic requirements:
1. I do not want to be notified (no filesystemwatcher event then)
2. I do not want to read the whole file (no MD5 checksum then)

Thanks for all your replies

Tuesday, June 1, 2004

No need for .NET programming for computing hashes

"The File Checksum Integrity Verifier (FCIV) is a command prompt utility that computes and verifies cryptographic hash values of files. FCIV can compute MD5 or SHA-1 cryptographic hash values. These values can be displayed on the screen or saved in an XML file database for later use and verification.";en-us;841290

Just me (Sir to you)
Tuesday, June 1, 2004

No need to use .NET to watch file systems either.

Jan Derk
Tuesday, June 1, 2004

Are you male or female Jan?

Tuesday, June 1, 2004

look into md5sum.

Tuesday, June 1, 2004

The guy asked to not have to read the contents of the file. Anytime you do a checksum/hash, you're reading the contents of the file, no?

Tuesday, June 1, 2004

Yes, the OP did specify that requirement. However, given that md5sum can do its "thing" extremely quickly, it may be useful to either the OP or others reading this topic. Just depends on where you are placed on the accuracy vs speed trade-off

Wednesday, June 2, 2004

*  Recent Topics

*  Fog Creek Home