• Post Reply Bookmark Topic Watch Topic
  • New Topic

Directory last updated does not always change.  RSS feed

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I realize that this is not truly a java problem/question but I imagine that someone here has run into this before.

If I have the following directory structure"

Docs
|
---> Work
---> Home

If I add/remove a file/directory from the Work directory its last updated date changes, but the last updated data for Docs does not change.

The case I have the directory structure is much bigger, but same problem. I have a top level directory like 'Docs' and I need to know if any files/directories have been added/removed in any part of that directory tree.

I have thought of one possible solution and was wondering what others think or of anyone has had success with another solution.

I was going to recursively parse the entire tree starting at 'Docs' and store every file/directory in a list. Then iterate the list adding up all the last modified dates. (probably subtract off the smallest last modified form each so that the number does not get too big). If the sum of all last modified dates is different than a previous calculation then a directory/file has been changed.

As you can probably tell from this solution I do not need to know which directory/file changed, just that one or more have changed.

Thanks in advance.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The last modified reflects only the 1st level changes. In the past I have used JNotify to monitor changes (don't know if that will suit your requirement). I think Java7 has some similar built in functionality like JNotify but I haven't migrated to 7 yet.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 7 has interface java.nio.file.WatchService, but I haven't worked with it before.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore David Williams wrote:I was going to recursively parse the entire tree starting at 'Docs' and store every file/directory in a list. Then iterate the list adding up all the last modified dates. (probably subtract off the smallest last modified form each so that the number does not get too big). If the sum of all last modified dates is different than a previous calculation then a directory/file has been changed.

Why don't you keep a record of when you last ran the check. The next time you run it you only need to check for files with a later date - you can then stop iterating through the list as soon as you find one.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Why don't you keep a record of when you last ran the check. The next time you run it you only need to check for files with a later date - you can then stop iterating through the list as soon as you find one.

There is one caveat though - if there is a file with a date in the future (this might be due to changing the system time, or copying the file from a system which had system time incorrectly set, there is also NTFS/FAT32 date inconsistency after daylight saving changes, etc.), this check might fail to detect a change (edit: it would actually detect a change every time it would be run).

(We've a system running 24/7 and system clock changes on Windows due to daylight saving time adjustments had caused us some headache in the past.)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!