# Adding quotes in a custom command

**URL:** <https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662>\
**Category:** Code\
**Created:** [September 12, 2024, 3:15pm UTC](https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662 "2024-09-12T15:15:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![artu72](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artu72/32/4929_2.png) [@artu72](https://discourse.cmake.org/u/artu72)\
**Post date:** [September 12, 2024, 3:15pm UTC](https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662/1 "2024-09-12T15:15:19Z")

</div>

Hi,

in my project I need to read content from a file VERSION ( a simple string line without spaces or newlines ), adding quotes (") at begin and end of file, and write it in a new file VERSION\_Q. E.g, if VERSION is 3.0, VERSION\_Q will be “3.0”

I have inserted the line in the add\_custom\_command block:

` COMMAND ${CMAKE_COMMAND} -E cat ${VERSION_FILE} > ${VERSION_Q_FILE}`

but this line simply copies VERSION file to VERSION\_Q. How to add quotes here?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 13, 2024, 10:11am UTC](https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662/2 "2024-09-13T10:11:52Z")

</div>

You probably can achieve that with a one-liner too, but I would do it like this:

```auto
file(READ "${VERSION_FILE}" VERSION_CONTENT)
# the file might (should) contain a empty line in the end of it
string(STRIP "${VERSION_CONTENT}" VERSION_CONTENT)
# perform some other checks like a RegEx for the version format
# ...
file(WRITE "${VERSION_Q_FILE}" "\"${VERSION_CONTENT}\"\n")

```

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [September 14, 2024, 12:36pm UTC](https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662/3 "2024-09-14T12:36:50Z")

</div>

The first two lines can be simplified more:

```CMake
file(STRINGS "${VERSION_FILE}" VERSION_CONTENT LIMIT_COUNT 1 NO_HEX_CONVERSION)

```

The command even has some `REGEX` option.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [September 16, 2024, 11:57am UTC](https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662/5 "2024-09-16T11:57:20Z")

</div>

> [@artu72](#):
>
> I would like ask a new question now: how I can introduce dependencies?

MODERATOR NOTE: Please open a separate discussion thread for new questions rather than changing the focus of an existing thread.

---

<div class="post-metadata">

**Author:** ![artu72](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artu72/32/4929_2.png) [@artu72](https://discourse.cmake.org/u/artu72)\
**Post date:** [September 16, 2024, 1:04pm UTC](https://discourse.cmake.org/t/adding-quotes-in-a-custom-command/11662/6 "2024-09-16T13:04:01Z")

</div>

Thank you, @retif and @jtxa . Both proposals are valid, I have selected @retif one since I prefer more simpler commands.

I would like ask a new question now: how I can introduce dependencies?

In my project, a source file depends upon the VERSION\_Q\_FILE, that depends in turn upon the VERSION\_FILE. I have tryed to write it using add\_dependencies command, but I cannot formulate them correctly.

> [@artu72](#):
>
> I would like ask a new question now: how I can introduce dependencies?
> 
> In my project, a source file depends upon the VERSION\_Q\_FILE, that depends in turn upon the VERSION\_FILE. I have tryed to write it using add\_dependencies command, but I cannot formulate them correctly.

I apologize: I am removing it and opening a new topic.
