This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting Files and Subdirs  RSS feed

 
Ivo Delver
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't find anything in the search, so i will try it this way
is it possible to count all files and subdirs with Java File
Tnx
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will probably be moved over to one of the Java in General forums ...
Look into the File class. Say aDirectory is a String that names a directory.

That's using recursion to drill into subdirectories. If that seems a little complex, stare at it for a while. If it doesn't get better, come back for more details.
}
 
Ivo Delver
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

"fileCount += countFilesIn( oneFile.getName() );"
this was the construction i was looking for to go deeper into the directory listing
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivo
If you are on Unix/Linux systems then you have to worry about "symbolic links" which might make the recursion never end...
So, we have to make sure that if the directory is already traversed then it should not be traversed again. To do this we have to apply the following logic,
- maintain java.util.Set of directories that are already traversed and put the canonical paths for those directories to that Set
- to know if a directory is a symbolic link or not we have to use CanonicalPath,
IF currFile.getCanonicalPath() is in Set already then its a symbolic link and we have to omit it
ELSE
- put directory's canonical path in the Set
- process the directory
HTH
Maulin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!