<jupyter_start><jupyter_text>How do I get a list of all files (including subfolders) that _match_ a particular metadata _property_? OverviewThis recipe is mainly used to show how to run through the project folder structure and find files of interest. Here we focus on listing all files within a single project that a particular metadata property, but same can be applied on other attributes, such as file name. One _use-case_ which will benefit greatly from this is: * I have _hundreds_ of files in my project * I want to run a task(s) which only uses _type X_ files * I want to query all _type X_ files with one call * I want to traverse the subdirectoriesYou can use the basic strategy to: 1. List all the files and folders (n = _N_) 2. Loop through the list  3. If item is a file - get the metadata of every file (as [here](files_detailOne.ipynb)), if the property matches, add it to a _list_ of files to process 4. Else if item is a folder start from 1. for that folder Prerequisites 1. You need your _authentication token_ and the API needs to know about it. See **Setup_API_environment.ipynb** for details. 2. You understand how to list projects you are a member of (we will just use that call directly and pick one here). 3. You have already cloned the Public Project _Cancer Cell Line Encyclopedia (CCLE)_.  ImportsWe import the _Api_ class from the official sevenbridges-python bindings below.<jupyter_code>import sevenbridges as sbg<jupyter_output><empty_output><jupyter_text>Initialize the objectThe `Api` object needs to know your **auth\_token** and the correct path. Here we assume you are using the credentials file in your home directory. For other options see Setup_API_environment.ipynb<jupyter_code># [USER INPUT] specify credentials file profile {cgc, sbg, default}
prof = 'default'

config_file = sbg.Config(profile=prof)
api = sbg.Api(config=config_file)<jupyter_output><empty_output><jupyter_text>Search by metadata<jupyter_code># [USER INPUT] Set metadata properties and values here; set project ID
project_id = 'my-name/my-project'
metadata_to_match = {'experimental_strategy': 'WXS',
                     'platform':'Illumina'}


def find_files_by_metadata(project_id, metadata_to_match, parent=None):
    """
    If parent is set, it is a folder id to search in.
    """

    
    if not parent:
        # Query by metadata in the root
        matched_files = list(api.files.query(
            project=project_id, limit=100, 
            metadata=metadata_to_match).all())
        for item in api.files.query(limit=100, project=project_id).all():
            if item.is_folder():
                matched_files.extend(find_files_by_metadata(project_id, metadata_to_match, item.id))
    else:
        # Query by metadata in the folder
        matched_files = list(api.files.query(
            parent=parent, limit=100, 
            metadata=metadata_to_match).all())
        for item in api.files.query(limit=100, parent=parent).all():
            if item.is_folder():
                matched_files.extend(find_files_by_metadata(project_id, metadata_to_match, item.id))
    return matched_files
            
    
matched_files = find_files_by_metadata(project_id, metadata_to_match)
print("Total matched files {}".format(len(matched_files)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07311893203883495, 'fraction_numerical': 0.006067961165048544, 'mean_word_length': 3.9135618479880776, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '439581', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 955, 'n_tokens_neox': 916, 'n_words': 376, 'path': 'Recipes/SBPLAT/files_listByMetadataRecursive.ipynb', 'repo_name': 'sbg/okAPI', 'size': '3296'}