在批处理文件中用%替换特殊字符

时间:2015-06-24 11:19:56

标签: windows batch-file command-prompt

任何人都可以帮助如何替换包含空格,(, - ,等)等特殊字符的字符串。 让我们说我有一个字符串:

str=A Room Has (1 level) - (2 - leve) Rooms

Output string should be like:

str=A%Room%Has%%1%level%%%%2%%%leve%%Rooms

2 个答案:

答案 0 :(得分:4)

这应该有效:

1

答案 1 :(得分:2)

试试这样:

@echo off
set "str=A Room Has (1 level) - (2 - leve) Rooms"
echo Before string substitution :   %str%
pause
setlocal EnableDelayedExpansion
set "str=!str:-=%%!"
set "str=!str:(=%%!"
set "str=!str:)=%%!"
set "str=!str: =%%!"
Color 0A
echo After string substitution  :   !str!
pause