Reputation: 1540
I am trying to list all folders and subfolders in my ftp account in recursion. The function ftpPrintDirsList returns Array List of folders in the folder. If i have this folders: a/b/c, a/b/d/e, a/f/g/, a/f/g/h, a/f/g/i
I need to get Array List that looks like this: a, a/b, a/b/c, a/b/d, a/b/d/e, a/f, a/f/g, a/f/g/h, a/f/g/i (The order does not have to be that way, but i need to see all the folders)
So far i got stuck here:
ArrayList<String> fassD = ftpPrintDirsList("/FlashAssets");
ArrayList<String> inner = null;
for (int i = 0; i < fassD.size(); i++) //for each subfolder in FlashAssets
{
inner = ftpPrintDirsList("/FlashAssets/"+fassD.get(i)); //list all subfolders
for (int j = 0; j < inner.size(); j++)
{
inner.set(j, fassD.get(i)+"/"+inner.get(j));
fassD.addAll(inner);
}
}
Log.d(TAG, "Final: "+fassD);
But i get an infinite loop, can anyone help me please. Thank you in advance.
Upvotes: 0
Views: 460
Reputation: 5094
Here's a recursive method, assuming that ftpPrintDirsList does what it describes. For each folder, it adds itself to the list, then calls the method for all subfolders. When there are no subfolders, it will add itself to the list then return. This will give you a depth first ordering of your files. /a/b/c comes before a/c
public ArrayList<String> getFolders(String foldername)
{
ArrayList<String> retval = new ArrayList<String>();
retval.add(foldername);
ArrayList<String> subfolders = ftpPrintDirsList(foldername);
for (String folder : subfolders)
retval.addall(getFolders(foldername + folder));
return retval;
}
Upvotes: 1