我的Windows批处理文件中有以下字符串:
"-String"该字符串还包含字符串开头和结尾的twoe引号,正如上面所写的那样。
我想去掉第一个和最后一个字符,以便得到以下字符串:
-String我试过这个:
set currentParameter="-String"
echo %currentParameter:~1,-1%这将打印出应该是的字符串:
-String但是,当我尝试像这样存储经过编辑的字符串时,它会失败:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%什么都不会被打印出来。我做错什么了?
这真的很奇怪。当我移除像这样的字符时,它起作用了:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%打印出来:
-String但实际上,我的批次有点复杂,在那里它不起作用。我会展示我的程序:
@echo off
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=%currentParameter:~1,-1%
echo String WITH stripping characters: %currentParameter%
echo .
)
exit /b
:end产出如下:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters:
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: ~1,-1
.但我想要的是
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters: -String
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: -String2
.发布于 2014-03-07 04:54:54
希望这能帮到你。
setlocal enabledelayedexpansion
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter! 发布于 2013-07-09 10:46:09
在括号大小的块中移动一个变量。注意-新值将不会在同一个块中使用(除非您用!而不是%-并在enabledelayedexpansion模式下运行)。或者只将这一对行提取到另一个子函数中,使用普通的行序列()
你好,斯塔奇
发布于 2016-01-02 21:23:03
这个脚本利用了ENABLEDELAYEDEXPANSION。如果您不知道,批处理脚本将执行for和If命令;因此,如果您这样做了:
if true==true (
@echo off
set testvalue=123
echo %testvalue%
pause >NUL
)您不会输出任何内容,因为在执行echo % testvalue %时,它没有识别出已更改的testvalue。使用delayedexapnsion可以让脚本像现在一样读取该值,并忘记我前面提到的问题。您使用它就像%testvalue%,但您可以这样做!测试值!要解决这个问题:
if true==true (
@echo off
set testvalue=123
echo !testvalue!
pause >NUL
)@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter!
echo .
)
exit /b
:end阿历克斯
https://stackoverflow.com/questions/12074510
复制相似问题