aspose file tools*
The moose likes I/O and Streams and the fly likes Read subdirectories and count files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read subdirectories and count files?" Watch "Read subdirectories and count files?" New topic
Author

Read subdirectories and count files?

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Hello
i need some help on counting total files in each subdirectories . I already has a method reading the directories, but not sure how to count file for each subdirectories. I need to have the output like this. For example I read from c:\temp

result will like this:
c:\temp\one\ 3 (assume this sub-directory have 3 files)
c:\temp\one\two 10 (assume this sub-directory have 10 files)
c:\temp\one\two\three 4 (assume this sub-directory have 4 files)

You help is greatly appreciated.
Regards.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
So what's this getFileListing() method you're calling? It looks like you're missing a method. And what happens when you run the code you have now? How are the results different from what you expect?


"I'm not back." - Bill Harding, Twister
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Sorry for a late response, i was tight up with something else, and now I can revisit the problem.

the getFiles( File aStartingDir ) // giving me a list of directory including the directory and files name , so how do I do the count for how many files that are in the sub-directory under the parent directory
This is the out put of the getFiles method, How do I count each of the line and display the total files under each directory?
c:\temp\dir1\test1.txt
c:\temp\dir1\test2.txt
c:\temp\dir1\dir2\text3.txt
c:\temp\dir1\dir2\text4.txt
c:\temp\dir1\dir2\dir3\test5.txt

so the result should be like:
c:\temp\dir1\ 2 files
c:\temp\dir1\dir2\ 2 files
c:\temp\dir1\dir2\dir3\ 1 file

Any help or hints is greatly appreciated. Regards
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
I am trying to count the totol files in each directory and print out,

Please help ! Thanks in advance

I do: java ListAllFiles c:\temp -----> it is giving NullPointerException?

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

It is always polite to share the exception stack trace as that contains the line number the exception occurs on.
One very useful debugging technique is to sprinkle System.out.println() statements throughout one's code. I always like to print out method calls and their parameters as well as important variables that I'm examining, like, say, if a file is a directory or not. Why don't you try this technique and share the results with us?


[How To Ask Questions On JavaRanch]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Note that the stack trace contains the line number of the exception, but that line number won't mean anything to us because we don't have your complete file. We can't tell what the line numbers mean. But if your stack trace starts off with

that tells you to look at ListAllFiles.java, line 42. Tell us which line that is, and we might be able to help.

Also, I don't think this will explain your NullPointerException, but here's another problem:

When you call oneFile.getName(), you get only the name of the file, not the directory it's in. So the countFilesIn() methods won't be able to find the files, because it doesn't know where they are. I recommend you change the method to take a File parameter rather than a String - that way the File object still has all the info you need to find the file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read subdirectories and count files?