# How to set the FOLDER property for all Targets in a given subdirectory

**URL:** https://discourse.cmake.org/t/how-to-set-the-folder-property-for-all-targets-in-a-given-subdirectory/3384
**Category:** Usage
**Created:** [May 21, 2021, 4:20pm UTC](https://discourse.cmake.org/t/how-to-set-the-folder-property-for-all-targets-in-a-given-subdirectory/3384 "2021-05-21T16:20:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RichTea](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@RichTea](https://discourse.cmake.org/u/RichTea)
#### Post date: [May 21, 2021, 4:20pm UTC](https://discourse.cmake.org/t/how-to-set-the-folder-property-for-all-targets-in-a-given-subdirectory/3384/1 "2021-05-21T16:20:56Z")

</div>

We have an uber-project that brings in several sub-projects via add\_subdirectory().  
Several of these creates a large number of static library Targets which they then assemble into the dynamic libraries and executables that we actually consume in the uber-project.

This results in the UI of the Visual Studio IDE “Solution” pane showing all the targets in a flat list, including all the ‘private’ static libraries for each of the subprojects.  
As there are a really large number of Targets in some of the sub-directories, this makes it quite difficult to quickly find the proper sub-project.

I’d like to organise them into folders, however the only option I’ve found so far is set\_target\_properties(list of targets PROPERTIES FOLDERS “folder”)

While that works, it’s not really maintainable because I have to list all the Targets created by the sub-project, most of which are static libraries whose names are effectively “hidden” from the uber-project.

Is there a way to set the FOLDER property for _all_ the targets created via add\_directory(), so I can place the whole thing into an appropriate folder without needing to know the names of any of the private targets?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 21, 2021, 4:26pm UTC](https://discourse.cmake.org/t/how-to-set-the-folder-property-for-all-targets-in-a-given-subdirectory/3384/2 "2021-05-21T16:26:09Z")

</div>

A suitable approach is described in [this reply](https://discourse.cmake.org/t/clang-format-integration/3358/8), you can instead set the `FOLDER` property instead.

---

<div class="post-metadata">

### Author: ![tuket](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tuket/32/4580_2.png) [@tuket](https://discourse.cmake.org/u/tuket)
#### Post date: [May 10, 2024, 8:12am UTC](https://discourse.cmake.org/t/how-to-set-the-folder-property-for-all-targets-in-a-given-subdirectory/3384/3 "2024-05-10T08:12:02Z")

</div>

I used this function I found in a gist: [Find all CMake targets declared in a directory (recursively) · GitHub](https://gist.github.com/mikedld/a687baabc952a7f061b6a2fd3069f91c)

```cmake
function(print_all_targets DIR)
    get_property(TGTS DIRECTORY "${DIR}" PROPERTY BUILDSYSTEM_TARGETS)
    foreach(TGT IN LISTS TGTS)
        message(STATUS "Target: ${TGT}")
        # TODO: Do something about it
    endforeach()

    get_property(SUBDIRS DIRECTORY "${DIR}" PROPERTY SUBDIRECTORIES)
    foreach(SUBDIR IN LISTS SUBDIRS)
        print_all_targets("${SUBDIR}")
    endforeach()
endfunction()

print_all_targets(myFolder)

```

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [May 10, 2024, 2:06pm UTC](https://discourse.cmake.org/t/how-to-set-the-folder-property-for-all-targets-in-a-given-subdirectory/3384/4 "2024-05-10T14:06:12Z")

</div>

The target property `FOLDER` is pre-initialised from the variable `CMAKE_FOLDER` if that is set when the target is created (see its [docs](https://cmake.org/cmake/help/latest/variable/CMAKE_FOLDER.html)). So, in order to store all targets from a subdirectory in a particular folder, all you have to do is this:

```auto
set(CMAKE_FOLDER FolderFor/Subdir)
add_subdirectory(sub/dir)
unset(CMAKE_FOLDER)

```
