0

I have a CMake project and I need to copy files from one folder to another and do some additional renaming with the following pattern:

"xy-1" will be "abcd1", "xy-2" will be "abcd2", ..., "xy-123" will be "abcd123"

Currently I´m trying this with add_custom_command() and in general it works for a simpler pattern like

"xy-1" will be "abc1", "xy-2" will be "abc2", ..., "xy-123" will be "abc123",

I´m doing it with

COMMAND copy "${path_to_source_folder}xy-*.txt" "${path_to_destination_folder}abc*.txt"

In the working pattern the number of characters before the asterisk are the same for the source- and the destination-name.

But in the not working pattern, there are 3 characters in the source-name and four characters in the destination-name before the asterisk:

COMMAND copy "${path_to_source_folder}xy-*.txt" "${path_to_destination_folder}abcd*.txt"

This not working. There is no error coming from cmake, but it will not copy all files (only the first 99) and the names are not created properly.

A quick fix would be to list all (400+) files explicitly, but I´d like to avoid this ;-)

How to do this with add_custom_command() or if this is not possible, how to do it in another way?

Many thanks for your help!

Cheers Daniel

Upate: Thanks for the hints in the comments! I´m running on Windows 10 and using powershell for running cmake.

Daniel D.
  • 73
  • 1
  • 7
  • 1
    The `COMMAND` just executes shell script. So the primary question would be "How to perform the task in the shell script". By the way, you haven't specified the OS, but when writting the shell scripts it is very important information. Are you on Windows (which has `copy` command)? Please, add needed information into the question post. – Tsyvarev Jun 25 '19 at 16:09
  • 1
    Do you know the count of files in advance? If you know, maybe you could try `foreach(...) execute_command(COMMAND copy "xy-count.txt" "abcd${count}.txt")`. If not, I think, you have to write a shell script. And a script is OS dependent. You could use utilities like [rename](http://man7.org/linux/man-pages/man1/rename.1.html). – KamilCuk Jun 25 '19 at 16:40
  • @Tsyvarev Thanks for your hint! I´m running on Windows 10 and using powershell for running cmake. When I run cmake with ```COMMAND [command]```, which shell is it using? The one in which I´ve started cmake? – Daniel D. Jun 26 '19 at 06:59
  • Hm, actually I don't know how CMake chooses shell for `COMMAND`. Probably, it depends from the [CMake generator](https://cmake.org/cmake/help/v3.7/manual/cmake-generators.7.html) you use (it is better to include name of the generator to the question post too). But it is possible that calling cmake from powershell affects on the shell selected. You may try to use a COMMAND, which output differs in powershell and common `cmd`. See e.g this question: https://stackoverflow.com/questions/53447286/in-a-cmd-batch-file-can-i-determine-if-it-was-run-from-powershell – Tsyvarev Jun 26 '19 at 08:22
  • For linux I solved it with a loop and rename, thanks for your hints. For the windows site I have to figure out some solution... – Daniel D. Jun 27 '19 at 12:18
  • Same thing (loop with renaming) is working in Windows :-) – Daniel D. Jul 19 '19 at 11:32

0 Answers0