List of Files in Assets Folder and Its Subfolders

List of files in assets folder and its subfolders

private boolean listAssetFiles(String path) {

String [] list;
try {
list = getAssets().list(path);
if (list.length > 0) {
// This is a folder
for (String file : list) {
if (!listAssetFiles(path + "/" + file))
return false;
else {
// This is a file
// TODO: add file name to an array list
}
}
}
} catch (IOException e) {
return false;
}

return true;
}

Call the listAssetFiles with the root folder name of your asset folder.

    listAssetFiles("root_folder_name_in_assets");

If the root folder is the asset folder then call it with

    listAssetFiles("");    

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

android list files contained in assets subfolder

You'll probably want to do this:

private void listFiles(String dirFrom) {
Resources res = getResources(); //if you are in an activity
AssetManager am = res.getAssets();
String fileList[] = am.list(dirFrom);

if (fileList != null)
{
for ( int i = 0;i<fileList.length;i++)
{
Log.d("",fileList[i]);
}
}
}

Also your function call should be: listFiles("images"); if you want to list images.

How can I loop over sub folders in Assets folder?

You could simply make it recursive using List<T>.AddRange like

private static string[] GetSubFoldersRecursive(string root)
{
var paths = new List<string>();

// If there are no further subfolders then AssetDatabase.GetSubFolders returns
// an empty array => foreach will not be executed
// This is the exit point for the recursion
foreach (var path in AssetDatabase.GetSubFolders(root))
{
// add this subfolder itself
paths.Add(path);

// If this has no further subfolders then simply no new elements are added
paths.AddRange(GetSubFoldersRecursive(path));
}

return paths.ToArray();
}

So e.g.

[ContextMenu("Test")]
private void Test()
{
var sb = new StringBuilder();

var folders = SubFolders("Assets");

if(folders.Length > 0)
{
foreach (var folder in SubFolders("Assets"))
{
sb.Append(folder).Append('\n');
}
}
else
{
sb.Append(" << The given path has no subfolders! >>");
}

Debug.Log(sb.ToString());
}

will print out the entire project's folder structure.

For

Sample Image

I get

Assets/Example 1
Assets/Example 1/SubFolder A
Assets/Example 1/SubFolder B
Assets/Example 1/SubFolder C
Assets/Example 2
Assets/Example 2/SubFolder A
Assets/Example 2/SubFolder A/SubSubFolder A

So in your case it would be

string selectedPath = GetPath();
var folders = SubFolders(selectedPath);
foreach(var path in folders)
{
...
}

Flutter read all files from asset folder

Even if you specified only asset folders in pubspec.yaml, the flutter compiler lists all files from these folders in AssetManifest.json file. All we have to do is read this file:

final manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final images = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/images'));

How can I add all files in sub-directories of assets in pubspec.yamal at once?

By just listing the directory name instead of the file name

flutter:
uses-material-design: true
assets:
- assets/foo/
- assets/bar/

This works only for the files directly in the listed directory, not for files in directories below foo/ or bar/

Trying to access sub folders in assets folder

SOLVED

it was really simple..
to access the sub folders in assets folder you would need the following..

1st access the file by its file name..

    File file = new File(getFilesDir(), fname);

2nd while attempting to open the file use both file name and the url to the file

    in = assetManager.open(url+fname);

3rd in the intent use only the file name..(i was using the entire path)

    intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/"+fname),
"application/pdf");

How do I loop over all Assets folders and sub folders and get to a list all the prefabs in this folders?

This is what I wanted and working good for my case :

In the Assets right click on any Folder and then click on Add Components.
It will loop all the folders and sub folders of the folder you did right click Add Components. Then it will find all the prefabs add to them a Rigidbody and save the changes.

Depending on how many prefabs you have it might be a bit slowly but it's doing the job.

Things to add :

Options to add more components or multiple components.

Creating a log/text file with all the changes made prefabs names what changes made and when.

Undo if there an option to make undo on Assets or to make the undo using the text file reading the changes made and revert back.

Backup : Once adding components to make a backup first in a temp folder of the original prefabs (could be also use for undo cases ).

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using UnityEditor;
using UnityEngine;

public class AddComponents : Editor
{
private static string GetClickedDirFullPath()
{
string clickedAssetGuid = Selection.assetGUIDs[0];
string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
string clickedPathFull = Path.Combine(Directory.GetCurrentDirectory(), clickedPath);

FileAttributes attr = File.GetAttributes(clickedPathFull);
return attr.HasFlag(FileAttributes.Directory) ? clickedPathFull : Path.GetDirectoryName(clickedPathFull);
}

private static string GetPath()
{
string path = GetClickedDirFullPath();
int index = path.IndexOf("Assets");
string result = path.Substring(index);

return result;
}

static List<string> paths = new List<string>();
private static void GetFolders()
{

string selectedPath = GetPath();

string[] assetsPaths = AssetDatabase.GetAllAssetPaths();

foreach (string assetPath in assetsPaths)
{
if (assetPath.Contains(selectedPath))
{
if (assetPath.Contains("Prefabs"))
{
paths.Add(assetPath);
}
}
}
}

[MenuItem("Assets/Add Components")]
public static void AddComponents()
{
GetFolders();

for (int i = 0; i < paths.Count; i++)
{
if (File.Exists(paths[i]))
{
GameObject contentsRoot = PrefabUtility.LoadPrefabContents(paths[i]);

// Modify Prefab contents.
contentsRoot.AddComponent<Rigidbody>();

// Save contents back to Prefab Asset and unload contents.
PrefabUtility.SaveAsPrefabAsset(contentsRoot, paths[i]);
PrefabUtility.UnloadPrefabContents(contentsRoot);
}
}
}
}


Related Topics



Leave a reply



Submit