Counting the files in a folder

edited June 2015 in How To...

I am not very good at managing data.

I am saving some coordinates, that i need to load in a number of .txt files, in my sketch's Data folder, it looks a little like this:

countThese

How many .txt folders there are in different each folder is not fixed, the one on the image has only 2 files i that shall be loaded, but some other has 3, 4 or 5. (and possiply some will have even more, but what i know for sure is that none will have only one)

To load all the files, i must know how many files there are. As you can see, my solution was to add a file named »steps.txt« containing only one int: the number of files to be loaded. In the case shown on the picture, 2:

int numberOfFiles = int(loadStrings("data/folderName/steps")[0]);

But, it would be much easier, if there was a way to count the number of files in a folder, something like:

int numberOfFiles = getFileCount("data/folderName");

And i am also very sure that there is a way to do that (a library or something like that), i just don't know how.

Answers

  • edited November 2014 Answer ✓

    You're gonna need a FilenameFilter instance used as argument for File's list() method:

    You can check out an example of it in this post link:
    http://forum.processing.org/two/discussion/6677/splitting-a-text-file-using-crlf-and-parsing-file-

    Here's a custom FilenameFilter that would work for ya:

    import java.io.FilenameFilter;
    
    static final FilenameFilter FILTER = new FilenameFilter() {
      static final String NAME = "step", EXT = ".txt";
    
      @ Override boolean accept(File path, String name) {
        return name.startsWith(NAME) && name.endsWith(EXT);
      }
    };
    

    And a sample on how to use File's list() method:

    File f = dataFile("folderName");
    String[] names = f.list(FILTER);
    printArray(names);
    
  • Answer ✓

    ah, and then just names.length ?

  • Thanks, now it works.

    Not that having a .txt file containing the number of files in the directory as an int didn't work, but this looks better, both in the code, and in my data folder

  • edited November 2014

    Oh, forgotten to tell if we're sure all of the files within a folder is what we want, FilenameFilter is unnecessary. 8-|
    A simple list() w/o any arguments is pretty enough: *-:)
    https://docs.oracle.com/javase/8/docs/api/java/io/File.html#list--

  • I removed the FilenameFilter, and used only file.list()... and it always returned a number 1 greater than the actual number of files in the.

    It turns out that every single folder (even empty ones) contains a file called ".DS_store" (that Apple for some reason doesn't think i want to know is there)

    i suptract one from the total number of files, and it works perfectly.

  • edited November 2014

    I subtract 1 from the total # of files, and it works perfectly.

    Problem is that your sketch will only work correctly in MacOS w/o FilenameFilter I'm afraid! :-SS

  • Would be better to exclude all files whose name starts with a dot...

  • edited November 2014

    Would be better to exclude all files whose name starts with a dot...

    I don't have a Mac. Actually I'm not using any Windows, just Linux now! <):)
    But I guess the same way that Windows spreads those odious "Thumbs.db" system files in every folder w/ images in it, unless we disable it but we still got to delete them manually, Macs much probably will recreate those cretin ".DS_store" as well! X(

  • I have a question on that

    when I want to have jpg and JPG and bmp

    do I have to use , or ; to separate them ?

    both won't work

    static final String NAME = "", EXT = ".jpg;.JPG";

    I'm on Win 7

    thanks!

  • edited June 2015

    This is old and somehow I've missed it. :-\"
    Nonetheless @Chrisir, If it happens to be mixed lower & upper case letters, call toLowerCase() before endsWith() & startsWith(): :P

    import java.io.FilenameFilter;
    
    static final FilenameFilter FILTER = new FilenameFilter() {
      static final String NAME = "step", EXT = ".txt";
    
      @ Override boolean accept(File path, String name) {
        name = name.toLowerCase();
        return name.startsWith(NAME) && name.endsWith(EXT);
      }
    };
    
  • thank you!

  • edited June 2015

    @nikolaj== i had the same problem with .DS invisible file (apple) and it is more vicious than you say because this file is not always present: try with onyx you can verify e.g that it does not exist when you create a folder and appears only with some conditions (copy etc); what is sure is that when present it is always the first one in the folder: so i have made a little method which looks at the first element && if equals .DS or startsWith() skips this file: so you dont have too look the other files in the folder...

Sign In or Register to comment.