# Adding a new file to an existing source\_group

**URL:** <https://discourse.cmake.org/t/adding-a-new-file-to-an-existing-source-group/9977>\
**Category:** Code\
**Created:** [January 31, 2024, 4:33pm UTC](https://discourse.cmake.org/t/adding-a-new-file-to-an-existing-source-group/9977 "2024-01-31T16:33:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [January 31, 2024, 4:33pm UTC](https://discourse.cmake.org/t/adding-a-new-file-to-an-existing-source-group/9977/1 "2024-01-31T16:33:18Z")

</div>

I have a fairly large project which is structured in such a way that, after a large number of files have been generated, they are placed into a special ‘Generated’ folder within my Visual Studio project:

```auto
source_group(TREE ${CMAKE_CURRENT_BINARY_DIR}/GENERATED
             PREFIX "Generated"
             FILES ${SOURCE_FILES_GENERATED_FROM_BISON}
                   ${HEADER_FILES_GENERATED_FROM_BISON}
                   ${SOURCE_FILES_GENERATED_FROM_FLEX}
                   ${SOURCE_FILES_GENERATED_FROM_UI}
                   ${HEADER_FILES_GENERATED_FROM_UI}
                   ${MOC_FILES})

```

Much later in the project, after having gathered some other information which wasn’t available when the first files were generated, I generate one more file and I’d like to add it to the same Visual Studio project folder as the ones I generated earlier.

I can’t figure out a way of doing this. If I use the alternative syntax for the source\_group command, viz.

```auto
source_group(${target}/GENERATED/${target} 
                       FILES ${extraFile})

```

…then the new file is placed exactly where I want it to be, but all the ones that were there before get removed from the folder.

It looks that the two ways of using source\_group are mutually incompatible. Is that the case, or is there a way of doing this?

[I know that there is a workaround of retaining the list of files that I had when I executed the first source\_group command, and then doing a single invocation of source\_group using the ‘TREE’ syntax after the extra file has been generated. That would need quite a lot of refactoring, though, so I’d rather avoid it if there’s a simpler way.]

---

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [February 1, 2024, 1:59am UTC](https://discourse.cmake.org/t/adding-a-new-file-to-an-existing-source-group/9977/2 "2024-02-01T01:59:51Z")

</div>

I was slow on the uptake, but all I actually had to do was to add the extra file in a repeat of the original declaration, viz.

```auto
    source_group(TREE ${CMAKE_CURRENT_BINARY_DIR}/GENERATED
                 PREFIX "Generated"
                 FILES ${extraFile})

```

…and that did exactly what I needed.
