Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all the files from all the folder in a single list

Hi I am looking for the solution to list all the files from root/Android device.

Suppose there are 3 folder inside root directory,but I want to display all the files in all of these folder in a single list..

Now If am using

       File f=new File("/sdcard"); 

Then it will list all the files from the sdcard folder only..and If I will use

       File f=new File("/download"); 

Then it will list all the files from download folder only ..and if I will use

       File f=new File("/"); 

Then it will list only root direcoty files...not the files inside /sdcard or /download..

So what steps shall I follow to list all the files with a filter to list only .csv files from all the folder inside root.

Thanks..

like image 548
Kanika Avatar asked Mar 02 '12 09:03

Kanika


People also ask

How do you get a list of all files in a folder?

Press and hold the SHIFT key and then right-click the folder that contains the files you need listed. Click Open command window here on the new menu. A new window with white text on a black background should appear. o To the left of the blinking cursor you will see the folder path you selected in the previous step.

How do I get a list of files in multiple folders?

Select the Home tab. Click Select all in the ribbon, or press Ctrl + A to select all the files in the library view. Click on the Move To or the Copy To button, as per your requirement. The files in the Camera folder and its subfolders are now copied/moved to the destination; the folders remain.

How do I list all files in subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.

How do you get a list of all files in a directory in Python?

To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3. x.


1 Answers

Try this:

 .....  List<File> files = getListFiles(new File("YOUR ROOT"));   ....  private List<File> getListFiles(File parentDir) {     ArrayList<File> inFiles = new ArrayList<File>();     File[] files = parentDir.listFiles();     for (File file : files) {         if (file.isDirectory()) {             inFiles.addAll(getListFiles(file));         } else {             if(file.getName().endsWith(".csv")){                 inFiles.add(file);             }         }     }     return inFiles; } 

or variant without recursion:

private List<File> getListFiles2(File parentDir) {     List<File> inFiles = new ArrayList<>();     Queue<File> files = new LinkedList<>();     files.addAll(Arrays.asList(parentDir.listFiles()));     while (!files.isEmpty()) {         File file = files.remove();         if (file.isDirectory()) {             files.addAll(Arrays.asList(file.listFiles()));         } else if (file.getName().endsWith(".csv")) {             inFiles.add(file);         }     }     return inFiles; } 
like image 82
Vyacheslav Shylkin Avatar answered Oct 02 '22 00:10

Vyacheslav Shylkin