• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Directory last updated does not always change.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 7 has interface java.nio.file.WatchService, but I haven't worked with it before.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic