2

I need to get a LibGDX FileHandle for every file in a folder. I can do this using

FileHandle [] filehandles;
filehandles=Gdx.files.external("/").list();

to get all the files in a folder. However it doesn't give me sub-folders. I have considered using an if statement in a loop that goes through everything in the folder, checks for subfolders and then lists them too, but id need another array and i cannot be sure how many ill need. Also it would be inefficent.

Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46

1 Answers1

3

Here is a recursive solution. I didn't test it but it should work I think:

public void getHandles(FileHandle begin, Array<FileHandle> handles)
{
    FileHandle[] newHandles = begin.list();
    for (FileHandle f : newHandles)
    {
        if (f.isDirectory())
        {
            Gdx.app.log("Loop", "isFolder!");
            getHandles(f, handles);
        }
        else
        {
            Gdx.app.log("Loop", "isFile!");
            handles.add(f);
        }
    }
}

Start it with getHandles(Gdx.file.external("/")). The handler to the root directory.

Just use one Array. Pass it to the method and the method will fill it with all handlers.


UPDATE: You need to call it with the right handle! Since it does matter if you are on an Android device or on the desktop. According to this question Libgdx How to get a list of files in a directory?

Call it like this:

    FileHandle dirHandle;
    if (Gdx.app.getType() == ApplicationType.Android)
    {
        dirHandle = Gdx.files.internal("data");
    }
    else
    {
        dirHandle = Gdx.files.internal("./bin/data");
    }
    Array<FileHandle> handles = new Array<FileHandle>();
    getHandles(dirHandle, handles);

If you use internal. Else use absolute instead of external. If you pass it an empty FileHandle it will not work.

According to the wiki:

Desktop (Windows, Linux, Mac OS X)

On a desktop OS, the filesystem is one big chunk of memory. Files can be referenced with paths relative to the current working directory (the directory the application was executed in) or absolute paths. Ignoring file permissions, files and directories are usually readable and writable by all applications.

Moreover:

External:
External files paths are relative to the SD card root on Android and to the home directory of the current user on desktop systems.

Checked the function with an internal an absolute and also with an external. As mentioned take care that the external is relative to your user directory! e.g. C:/Users/user/... Just pass the right handle to it.

Community
  • 1
  • 1
bemeyer
  • 6,154
  • 4
  • 36
  • 86
  • I'd pass the array into the method. – nEx.Software Jan 24 '14 at 14:26
  • i think it does not really matter – bemeyer Jan 24 '14 at 14:57
  • 2
    It is good practice. There is zero benefit to create a new array for each recursion when its only purpose is to have all elements added to another array. If it doesn't cost anything to do it the right way, you should. – nEx.Software Jan 24 '14 at 16:10
  • I seem to be gettig a NPE on handles.add(f) – Hamzah Malik Jan 24 '14 at 19:48
  • Ok im not getting an NPE if i use /FOLDER_NAME in stead of / for the directory. But now it does nothing, it doesnt even run the for loop, as i have used println to check – Hamzah Malik Jan 24 '14 at 20:22
  • Yea because you need to give him the right Filehandle to start! Check the update. Add the logger to see if its even a directory what you pass him. If not your starting handle is not correct or it is a file not a folder. – bemeyer Jan 25 '14 at 10:21
  • Note the updated code with `.bin/data` is a hack that only works when running from Eclipse. – nEx.Software Jan 25 '14 at 12:17