# Prevent creating empty directories using install(DIRECTORY)

**URL:** https://discourse.cmake.org/t/prevent-creating-empty-directories-using-install-directory/4528
**Category:** Usage
**Created:** [November 23, 2021, 10:42am UTC](https://discourse.cmake.org/t/prevent-creating-empty-directories-using-install-directory/4528 "2021-11-23T10:42:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jjELT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjELT](https://discourse.cmake.org/u/jjELT)
#### Post date: [November 23, 2021, 10:42am UTC](https://discourse.cmake.org/t/prevent-creating-empty-directories-using-install-directory/4528/1 "2021-11-23T10:42:20Z")

</div>

When using [install(DIRECTORY … […])](https://cmake.org/cmake/help/latest/command/install.html#directory), how can I prevent empty directories from being created?

For instance, when installing all header files and template implementation files using

```auto
# Install the header files and template implementation files (.tcc).
install(DIRECTORY "src/"
		DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${TARGET_NAME}
		FILES_MATCHING
		PATTERN "*.h"
		PATTERN "*.tcc"
	)

```

the entire folder structure is reproduced, even though in some folders there might only be `.cpp` files in them and therfore they do not need to be created.

If I know the name of the folder to exclude, e.g. “FolderWithCppOnly” I can explicitly exclude it using

```auto
		PATTERN "FolderWithCppOnly" EXCLUDE

```

but I would have to manually add them one by one and keep track of name changes.

I also tried using regular expressions like so

```auto
            REGEX "\.h"
            REGEX "\.tcc"
            REGEX ".*[^(\.h)]$" EXCLUDE
            REGEX ".*[^(\.tcc)]$" EXCLUDE

```

but it does not work.

Is there a way to exclude all folders and only create them if there are actually matched files in them?

---

<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: [November 24, 2021, 2:27am UTC](https://discourse.cmake.org/t/prevent-creating-empty-directories-using-install-directory/4528/2 "2021-11-24T02:27:12Z")

</div>

I think this probably needs a new keyword like `IGNORE_EMPTY_DIRECTORIES` due to compatibility reasons. Note that we’d also need to prune intermediates that we might not know are empty until later:

```auto
foo/
foo/bar/
foo/bar/ignored

```

will install `foo/`, `bar/`, ignore `ignored`, realize `bar/` is empty, then realize `foo/` is empty and need to remove it. Note that this is just algorithmically; the code can build up this TODO list and execute it later to avoid actually making and removing the intermediate results.

---

<div class="post-metadata">

### Author: ![jjELT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjELT](https://discourse.cmake.org/u/jjELT)
#### Post date: [November 24, 2021, 7:51am UTC](https://discourse.cmake.org/t/prevent-creating-empty-directories-using-install-directory/4528/3 "2021-11-24T07:51:42Z")

</div>

I created an issue: [22947](https://gitlab.kitware.com/cmake/cmake/-/issues/22947)
