Pass double quotes to batch script with execute_process()

I’m trying to execute a batch script with an argument that requires double quotes, something like script.bat "foo=bar". If this argument is passed in without the double quotes, you end up with %1 being “foo” and %2 being “bar”, instead of the desired %1 being “foo=bar”.

With single double quotes around the argument (execute_process(COMMAND script.bat "foo=bar")) I get no quotes in the call. All other combinations of escaping seem to propagate the escape to the call itself, with backslashes in the arg.

Is this possible without writing an intermediary batch script to call this other batch script?

Your example just has a CMake string defined, without any quotes.
CMake just allows you to omit the " around string, as long as you don’t have whitespaces or semicolons as content.

Have you tried this: execute_process(COMMAND script.bat "\"foo=bar\"")
Or if there is no variable involved: execute_process(COMMAND script.bat [["foo=bar"]])

Craig Scott wrote a blog article, that might help you in understanding quoting:

Both of these escape options result in the batch script being called with "\"foo=bar\"", the desired result is "foo=bar".

Here is a complete example that demonstrates the problem:

cmake_minimum_required(VERSION 3.29)

project(quotes)

file(WRITE quotes.bat "echo %2")

# will print "bar", should print nothing
execute_process(COMMAND quotes.bat "foo=bar")

The output for this is bar, which is not the correct invocation of quotes.bat. The correct call to script.bat should be script.bat "foo=bar".

I need to invoke a batch script with an argument that contains verbatim double quotes surrounding an arg.

execute_process assumes that the child uses the MSVC C runtime library’s CommandLineToArgvW to parse the command line. In order to do anything different, a new option will be needed.

CMake Issue 16321 requests a way to control the raw command line on Windows.