在Windows批处理文件中删除字符串的第一个和最后一个字符

13

我在Windows批处理文件中有以下字符串:

"-String"

字符串还包含了在开头和结尾处的两个引号,就像上面所写的一样。

我希望剥离掉第一个和最后一个字符,以便得到以下字符串:

-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
.

1
你的示例代码(如预期)可以工作,但可能存在另一个错误。也许你尝试在块内运行它? - jeb
我收到了以下信息: ECHO ist ausgeschaltet (OFF). ~1,-1 - Metalhead89
也许是因为它在一个函数里面? `:myFunc FOR /F "tokens=%1 delims=," %%I IN ("%processChain1%") Do ( set currentParameter=%%I set currentParameter=%currentParameter:~1,-1% echo %currentParameter% rem java -jar app.jar %%I ) exit /b` - Metalhead89
2
原因是百分号扩展是在块的解析时间而不是执行时间。但在您的情况下,可以使用%%~I来解决...动态令牌计数的问题。 - jeb
只是这个微小的 ~ 就完成了工作...太棒了。非常感谢你。 - Metalhead89
4个回答

8
希望这能帮到您。
    setlocal enabledelayedexpansion
    
    echo String WITHOUT stripping characters: %%I 
    set currentParameter=%%I
    set currentParameter=!currentParameter:~1,-1!
    echo String WITH stripping characters: !currentParameter! 

3
你可能应该提到,这只有在启用了“延迟扩展”的情况下才有效,这可能会产生其他难以调试的副作用。 - Binarus

4

您正在括号块内修改变量。请注意,新值不会在同一块中使用(除非您使用!而不是%来限定变量并在启用延迟扩展模式下运行)。 或者将这几行提取到另一个子函数中,使用普通的行序列而不是()

问候,Stach


4

这个脚本利用了ENABLEDELAYEDEXPANSION。 如果您不知道,批处理脚本会一次性执行for和if命令。因此,如果您输入:

if true==true (
@echo off
set testvalue=123
echo %testvalue%
pause >NUL
)

您不会输出任何内容,因为当执行echo %testvalue%时,它没有识别到testvalue已经被更改。使用延迟扩展允许脚本按其当前值读取该值,并忘记我之前提到的问题。您可以像使用%testvalue%一样使用它,但是您可以使用!testvalue!来解决此问题:

if true==true (
@echo off
set testvalue=123
echo !testvalue!
pause >NUL
)
  • 会输出 123。

@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

~ Alex


0

我曾经遇到过类似的问题,但是通过去除以下空格来解决:

例如:set FileName=%Name:~0,11% # 没有'='前后的空格,可以正常工作

例如:set FileName = %Name:~0,11% # 在'='前或后有空格,无法正常工作

因此,请尝试去除空格,这样应该可以解决问题。 注意:需要重新打开命令行以刷新后台值,否则它将显示与存储在临时文件中相同的输出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接