sunny
sunny

Reputation: 1945

Comparing directories using Java: how could I recognize deleted files?

I compare 2 directories using Java 7. If somefile.txt exists in directory A, but doesn't exist in directory B, I'd like to distinguish between 2 cases:

  1. somefile.txt was at both directories before, and then it was removed from directory B.
  2. somefile.txt was just added to directory A (never been at directory B)

I cannot use any monitoring, manage logs\history. Also I'd like to find OS-independent solution.

Could I guess if somefile.txt is new or deleted by comparing of it's lastModified to lastModified of both folders in some way? I feel there is some simple algorithmic solution, but I cannot find it out...

Help...

Upvotes: 4

Views: 639

Answers (2)

burna
burna

Reputation: 2962

You can't say wether the file is new in B or was just deleted from A. Unless you have some logs or history of the folder and its content, then you could compare the history with the current state.

In your case, you have only the current state of the directories, there is no way to guess what happened before.

Upvotes: 2

radai
radai

Reputation: 24192

for that you'd need to determine which directory (A or B) is the newerr copy and which is the older copy. maybe you could take the latest file modification time from all the files in A, compare it with the same calculation for B and tell which tree is newer? then telling 1 and 2 apart is easy - if the file is in the newer directory but not in the older then it was created, otherwise deleted.

depending on your problem you might want to use file creation times and not modification times

Upvotes: 1

Related Questions