Count Files with Java Program: Easy & Fast

  • Context: Comp Sci 
  • Thread starter Thread starter Badger33
  • Start date Start date
  • Tags Tags
    Counting Java Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
9 replies · 3K views
Badger33
Messages
34
Reaction score
0
I am attempting to write a program to do the work for me. I started in the process by hand and it took about an hour to get through the letter a. So I have a folder with many sub-folders and each sub-folder has at least one sub-sub-folder and then in theses many sub-sub-folders are the individual files. I would like to write a program that will allow me to specify a folder and then it will do the work and return the value of the number of total items in those folders.

Homework Statement


Homework Equations


The Attempt at a Solution

 
Last edited:
Physics news on Phys.org
This is the start that I have.

public class Counting {
public static void main(String[] args){

}
}


I was thinking that it would be easier to write a separate class to handel most of the work. I don't even know how to access a file or how to write the call to ask for the user input into the location of the file that the user wants to be accessed.
 
Badger33 said:
I am attempting to write a program to do the work for me. I started in the process by hand and it took about an hour to get through the letter a. So I have a folder with many sub-folders and each sub-folder has at least one sub-sub-folder and then in theses many sub-sub-folders are the individual files. I would like to write a program that will allow me to specify a folder and then it will do the work and return the value of the number of total items in those folders.

Badger33 said:
This is the start that I have.

public class Counting {
public static void main(String[] args){

}
}


I was thinking that it would be easier to write a separate class to handel most of the work. I don't even know how to access a file or how to write the call to ask for the user input into the location of the file that the user wants to be accessed.

To elaborate what TylerH said, your problem description and the code you provide don't give us enough information about what you are trying to do. We don't know anything about the directory structure you have - what does the letter 'a' have to do with what you're trying to do. We can't help if we don't know what you're trying to do.
 
I don't really know what you want to know. I have done some basic java programming but I have never really touched this kind of input into a program so I do not know what you mean when you say directory. If yo can explain it then I can help and hopefully make what I am trying to do with this program more clear.
 
Folder - directory -- same same.
What we (TylerH and myself) want to know is what you are trying to do. Your description is so vague that we have no idea what you are describing. The first step in designing a program is understanding what the problem is you're trying to solve. Your first post doesn't give us much of a clue.

You said
it took about an hour to get through the letter a.
We don't know what you're talking about.
 
ok well I want to sep through all of the folders. Just keep working into each of them and then when there are no further folders then at that point I want to count the items in those folders. I have an example I want the program to return the number 27 because that's the number of individual items. So the file I want to pass to the program is the one at the top and is called Input folder. Obviously this is just a rough example I made up names for all the entries but it should give the general idea of the structure.
Yes in this example all of the files are the same kind however in the other real program there are a variety of different file formats.
 

Attachments

  • Example.jpg
    Example.jpg
    57.6 KB · Views: 489
So you want to count the number of files in a specific directory? Or do you want to give it a root directory, and count all the files in each sub sub folder?

In another word:
Input: directory
Output: 27 files

Or:
Input: root directory
Output:
Aab > folder4 : 27
Abc > folder 1: 1
 
Either one will work I guess. I am looking for just the final number. If I input the RootDirectory in the example Input Folder is what i want to put in. I am looking for the total so like

Ideally
Input: the path id for "Input Directory"
Output: 27 files
 
Well, you can get the starting directory from the parameters passed to the program. Or you can read it in with the Scanner class or similar.

Assuming you have a start directory, you can recurse through something like this:

Code:
    public void traverse(File f) {
        if (f.isDirectory()) {
            System.out.println("Directory: " + f);
            File[] contents = f.listFiles();
            for (File current : contents) {
                traverse(current);
            }
        } else {
            System.out.println("  File: " + f);
        }
    }

That's just a piece of what you'd need to write, but that's the main logic. You'll need to actually count things, of course. I hope you understand the recursion in there. If you can't figure it out, let us know, of course.