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.
104} How can one remove double quotes from a script variable?
There are different methods. First
@echo off & setlocal enableextensions
set var="a b c d"
echo %var%
set var=
%var
:"
=%
echo %var%
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
"a b c d"
a b c d
Note that ALL the quotes are removed. Also those potentially within
the string.
The other methods include
@echo off & setlocal enableextensions
::
set var="a b c d"
echo %var%
for %%f in (%var%) do echo %%~f
::
set var="cmdfaq.*"
echo %var%
for %%f in (%var%) do echo %%~f
::
set var=%var:"=%
echo %var%
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
"a b c d"
a b c d
"cmdfaq.*"
CMDFAQ.CMD
cmdfaq.*
Note how the asterisk * is expanded for an existing file name. In
fact, as seen in many examples, if you had
@echo off & setlocal enableextensions
set var="cmdfaq*.*"
echo %var%
for %%f in (%var%) do echo %%~ff
endlocal & goto :EOF
the output would be something like
C:\_D\TEST>cmdfaq
"cmdfaq*.*"
C:\_D\TEST\CMDFAQ THIRD.CMD
C:\_D\TEST\CMDFAQ.CMD
C:\_D\TEST\CMDFAQ2.CMD
Although not practical, for demonstration one can also have
@echo off & setlocal enableextensions
set var="a b c d"
echo %var%
for /f "tokens=*" %%f in ("%temp%") do set temp_=%%~sf
echo @set var=%var%|sed -e "s/\"//g">%temp_%\tmp.cmd
for %%c in (call del) do %%c %temp_%\tmp.cmd
echo %var%
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
"a b c d"
a b c d