抱歉,这似乎有点基础,但我对此很陌生。我以为我已经解决了所有问题,但当我将其移动到我需要运行的实际环境中时,它停止工作了。
一切都正常,直到我尝试拆分,"%result%"
此时我抓取了一行如下所示的文本
"Avanti B & C 3",9,2,18,1,11.625,"11 5/8",8.98438,"8 31/32",0.76563,"49/64",162,"Partition"...etc
我想检索 Avanti B & C 3,162,Partition。作为测试,我尝试了此方法,但无法让它返回第一个字符串。
set result="Avanti B & C 3",6,2,9,1,14.625,"14 5/8",82,"82",0.75,"3/4",72,"Unfinished Right End",2181,"3/4 2s Quartersawn Maple",0,1,1,0,0,0,0,0,0,0,0,0,"Tall Cabinet W/ routed pe","","078b0072","1LE","Library 1044","",0,0,"*078b0072*","","Default","N:\CVLabels\078p0072.jpg"
for /f tokens^=1^ delims^=^" %%i in ("%result%") do set _jobname=%%i
echo %_jobname%
pause
*经过编辑以达到我需要的目的
答案1
tokens=1
是默认的,不需要表达。
从包含有毒字符的字符串中剥离双引号时,<>|&
需要先用插入符号进行转义。
问题在于%result%
用双引号括起来,这会在开头创建一对双引号,而&
没有用引号括起来,也没有进行转义。转义时请记住,一旦暴露出来,它就会丢失。
:: Q:\Test\2019\02\28\SU_1410257_2.cmd
@Echo off
set result="Avanti B & C 3",6,2,9,1,14.625,"14 5/8",82,"82",0.75,"3/4",72,"Unfinished Right End",2181,"3/4 2s Quartersawn Maple",0,1,1,0,0,0,0,0,0,0,0,0,"Tall Cabinet W/ routed pe","","078b0072","1LE","Library 1044","",0,0,"*078b0072*","","Default","N:\CVLabels\078p0072.jpg"
for /f delims^=^" %%i in ("%result:&=^&%") do set "_jobname=%%i"
echo %_jobname:&=^&%
pause
示例输出:
> Q:\Test\2019\02\28\SU_1410257_2.cmd
Avanti B & C 3
Drücken Sie eine beliebige Taste . . .
根据评论的要求编辑变体:
:: Q:\Test\2019\02\28\SU_1410257_3.cmd
@Echo off
set result="Avanti B & C 3",6,2,9,1,14.625,"14 5/8",82,"82",0.75,"3/4",72,"Unfinished Right End",2181,"3/4 2s Quartersawn Maple",0,1,1,0,0,0,0,0,0,0,0,0,"Tall Cabinet W/ routed pe","","078b0072","1LE","Library 1044","",0,0,"*078b0072*","","Default","N:\CVLabels\078p0072.jpg"
for /f "tokens=1,8,9delims=," %%A in ("%result:&=^&%") do (
set "_jobname=%%~A"
set "token8=%%B"
Set "token9=%%C"
)
echo _jobname=%_jobname:&=^&%
echo token8=%token8%
echo token9=%token9%
pause
示例输出:
> Q:\Test\2019\02\28\SU_1410257_2.cmd
_jobname=Avanti B & C 3
token8=82
token9="82"