List All Files in the Folder and Also Sub Folders

list all files in the folder and also sub folders

Using you current code, make this tweak:

public void listf(String directoryName, List<File> files) {
File directory = new File(directoryName);

// Get all files from a directory.
File[] fList = directory.listFiles();
if(fList != null)
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file.getAbsolutePath(), files);
}
}
}

List all files and folders (also Subfolders) in Assets Directory in my App and check if an asset is a file or a folder

Here is a solution to my problem that I found out working 100% listing all directories and files even sub-directories and files in subdirectories. It also works for multiple levels.

Note: In my case

  1. Filenames had a . in them. i.e. .htm .txt etc
  2. Directorynames did not have any . in them.

Note: about -- path --

  1. to get all files and directories in Assets root folder

    String path = ""; // assets

  2. or to get files and directories in a subfolder use its name as path

    String path = "html"; // <<-- assets/html

    listAssetFiles2(path); // <<-- Call function where required
    //function to list files and directories
    public void listAssetFiles2 (String path){
    String [] list;

    try {
    list = getAssets().list(path);
    if(list.length > 0){
    for(String file : list){
    System.out.println("File path = "+file);

    if(file.indexOf(".") < 0) { // <<-- check if filename has a . then it is a file - hopefully directory names dont have .
    System.out.println("This is a folder = "+path+"/"+file);
    if(path.equals("")) {
    listAssetFiles2(file); // <<-- To get subdirectory files and directories list and check

    }else{
    listAssetFiles2(path+"/"+file); // <<-- For Multiple level subdirectories
    }
    }else{
    System.out.println("This is a file = "+path+"/"+file);
    }
    }

    }else{
    System.out.println("Failed Path = "+path);
    System.out.println("Check path again.");
    }
    }catch(IOException e){
    e.printStackTrace();
    }

    }

Thanks

Command to list all files in a folder as well as sub-folders in windows

The below post gives the solution for your scenario.

dir /s /b /o:gn

/S Displays files in specified directory and all subdirectories.

/B Uses bare format (no heading information or summary).

/O List by files in sorted order.

Then in :gn, g sorts by folders and then files, and n puts those files in alphabetical order.

list.files() all files in directory and subdirectories

To list the matching files in all subdirectories, you can use recursive = TRUE in list.files()

list.files(pattern = "_input.txt$", recursive = TRUE)

How do I list all the files in a directory and subdirectories in reverse chronological order?

Try this one:

find . -type f -printf "%T@ %p\n" | sort -nr | cut -d\  -f2-

Method to get all files within folder and subfolders that will return a list

private List<String> DirSearch(string sDir)
{
List<String> files = new List<String>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
files.AddRange(DirSearch(d));
}
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message);
}

return files;
}

and if you don't want to load the entire list in memory and avoid blocking you may take a look at the following answer.

Listing all files in a directory and sub-directories C#

SearchOption.AllDirectories might do the trick for you as it includes the current directory and all its subdirectories in a search operation. This option includes reparse points such as mounted drives and symbolic links in the search.

//Add System.IO then
String[] allfiles = Directory.GetFiles("DirectorytoSearch", "*", SearchOption.AllDirectories);

and to convert them to list

List<string> allfiles = Directory.GetFiles("DirectorytoSearch", "*",SearchOption.AllDirectories).ToList();

How to get list of all subfolders in one folder and write it to txt file using vb

Whenever a problem is defined as "get list of all subfolders" and "write to a text file", I know I likely need to implement a loop of some kind. As it turns out that is all that is missing from your code. The Dir command can help solve this problem:

Private Sub Check()
Dim intFile As Integer
Dim strFile As String
Dim FolderName As String

MkDir "c:\New_Folder"
strFile = "c:\New_Folder\data.txt"
intFile = FreeFile
Open strFile For Output As #intFile
FolderName = Dir("c:\windows\", vbDirectory)

Do While FolderName <> ""
If FolderName <> "." And FolderName <> ".." And (GetAttr("c:\windows\" & FolderName) And vbDirectory) = vbDirectory Then
Print #intFile, FolderName
End If

FolderName = Dir()
Loop

Close #intFile
End Sub

I would also encourage you to use proper formatting of your code, in this case indentation. It will make your life easier at some point!

Python list directory, subdirectory, and files

Use os.path.join to concatenate the directory and file name:

for path, subdirs, files in os.walk(root):
for name in files:
print(os.path.join(path, name))

Note the usage of path and not root in the concatenation, since using root would be incorrect.


In Python 3.4, the pathlib module was added for easier path manipulations. So the equivalent to os.path.join would be:

pathlib.PurePath(path, name)

The advantage of pathlib is that you can use a variety of useful methods on paths. If you use the concrete Path variant you can also do actual OS calls through them, like changing into a directory, deleting the path, opening the file it points to and much more.



Related Topics



Leave a reply



Submit