当变量有内部空格时,如何调用concat:%variable%?

时间:2013-03-18 13:27:09

标签: batch-file cmd

我正在尝试使用以下命令在类路径中连接变量,但是当文件夹名称包含空格时它不起作用:

call concat : %variable%

其中%variable% ={folder name containing space}

我尝试过引号:

call concat : "%variable%"

但是这会在我的类路径中添加2个双引号,如下所示:

""folder name containing space""

:concat
set CLASSPATH=%CLASSPATH%;"%1"

3 个答案:

答案 0 :(得分:11)

在整个SET语句中加上引号:

SET "FOO=AB C"
SET "BAR=%FOO%D EF"
echo %BAR%

输出:

AB CD EF

答案 1 :(得分:6)

对于带空格的字符串,请勿使用其他双引号。参数%~删除字符串周围的所有双引号对:

@echo off &setlocal
set "Variable="my var""
echo Variable:  %Variable%
call :concat %Variable%
goto :eof

:concat
echo concat %%1:    %1
set "NewVar=%~1"
echo concat NewVar: %newvar%
goto :eof
endlocal

输出是:

Variable:       "my var" 
concat %1:      "my var" 
concat NewVar:  my var

如果在字符串周围添加其他双引号,则会发生以下情况:

@echo off &setlocal
set "Variable="my var""
echo Variable:  %Variable%
call :concat "%Variable%"
goto :eof

:concat
echo concat %%1:    %1
set "NewVar=%~1"
echo concat NewVar: %newvar%
goto :eof
endlocal

输出中断:

Variable:       "my var"
concat %1:      ""my
concat NewVar:  "my

答案 2 :(得分:1)

从输入中删除双引号,在整个集合中使用双引号,当使用连接字符串时,用双引号括起来,例如:

set root=%~1
@echo off
setlocal EnableDelayedExpansion
set i=0
for %%d in (
1.07 1.023 1.075 1.08 1.04
) do (
   set /A i=i+1
   set scale[!i!]=%%d
)
set i=0
for %%d in (
CO DE MH PL Pr
) do (
   set /A i=i+1
   set port[!i!]=%%d
)
for %%A in (0 1 2 3 4) do (
set "file1=%root%\Event\Events.txt"
set "file2=%root%\Port E\PortE_!port[%%A]!.txt"
set "file3=%root%\Poli E By Port\E_!port[%%A]!.txt"
%0\..\average101v2.exe "!file1!" "!file2!" "!file3!" !scale[%%A]!
)