This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
58} How do I best combine two quoted arguments into one quoted string?
Consider two cases. First, the arguments are in environment
variables:
@echo off & setlocal enableextensions
set x_="C:\Program Files\Schedule\"
set y_="My log.txt"
set z1_="%x_:"=%%y_:"=%"
echo.%z1_%
goto :EOF
The output will be
D:\TEST>cmdfaq
"C:\Program Files\Schedule\My log.txt"
Second, the argument are given as replaceable parameters to the
script:
@echo off & setlocal enableextensions
set z2_="%~1%~2"
echo.%z2_%
goto :EOF
The output will be
D:\TEST>cmdfaq "C:\Program Files\Schedule\" "My log.txt"
"C:\Program Files\Schedule\My log.txt"
The tilde ~ is essential in the second version. As the documentation
about "Using batch parameters" in "Command-line reference" says:
%~1 Expands %1 and removes any surrounding quotation marks ("").
Alternatively, (since e.g. NT4 does not recognize the ~trick) one
could use
@echo off & setlocal enableextensions
set x_=%1
set y_=%2
set z1_="%x_:"=%%y_:"=%"
echo.%z1_%
goto :EOF