• Post Reply Bookmark Topic Watch Topic
  • New Topic

Search for a file in a drive

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

What is the best method to search for a file using fileName? I agree could lots of files with same fileName across directory's.

Can I use lastModified Timestamp as my Filter? Has anybody attempted this before? Any solutions, suggestions regarding the same?

p.s: Why I need such a file is dsicussed in this post
JavaRanch DnD between OS & Swing... Hope this is not a cross-post between 2 forums

TIA
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't rely on the timestamp myself. Filesize is very useful if you know it. CRCs would be useful too, but probably a fair bit slower.
 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stuart

Can you elaborate more? Any programming samples that you could care to share here?

TIA
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I was presuming you already had some code to go through directories of files. Perhaps using the listFiles method of the File class.

In that case I was suggesting you use the file size as the main comparison method. IMHO timestamps are not reliable enough since (esp in Windows) they sometimes seem to change a lot. But the matching method also depends on your requirements: do you want to find only files that are the same and have the same name, or just files that are the same?

Anyway, you have the details of the file you want to find. You get a list of all files in the first directory (say C . If you encounter a file, you compare its name to the required file. If they match, you then compare their file sizes. If they also match, you might want to do an additional CRC32 check too. But we do that last because it is a fairly slow operation, so we only want to do it for files that are 'good suspects'. If all of these things match, you have (almost) certainly found a matching file.

Now, if during your travels you encounter a directory instead of a file, you just repeat the whole process again with the list of files from that directory. A recursive method in other words. Here is an outline (not tested or complete):
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!