aspose file tools
The moose likes Java in General and the fly likes Directory last updated does not always change. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Directory last updated does not always change." Watch "Directory last updated does not always change." New topic
Author

Directory last updated does not always change.

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8439

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.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Java 7 has interface java.nio.file.WatchService, but I haven't worked with it before.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
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.


Joanne
Martin Vajsar
Bartender

Joined: Aug 22, 2010
Posts: 2331
    
    2

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.)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Directory last updated does not always change.
 
Similar Threads
The ULTIMATE! From the creator of FileMonitor comes DirectoryMonitor :)
locking and the find method.
JTree collapses when reloaded (open problem)
how to find out if any files under directory recursively modified?
How to create a thread?