在批处理字符串中使用双引号

时间:2015-07-26 19:21:26

标签: batch-file

我试图编写一个批处理脚本来更改我的一个文件中的一行。

text.file:

ex1
ex2
<"ex3">
ex4

我想将<"ex3">替换为<"ex5">。我目前正在使用此代码:

@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION

rename text.file text.tmp
for /f %%a in (text.tmp) do (
    set foo=%%a
    if !foo!=="<"ex3">" set foo="<"ex5">"
    echo !foo! >> text.file) 
del text.tmp

如何转义"以便我可以将字符串与foo中的值进行比较?我尝试过使用\"^"""。前两个,我得The syntax of the command is incorrect,最后一个没有发生。

1 个答案:

答案 0 :(得分:2)

这应该有效

@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION

rename text.file text.tmp
for /f %%a in (text.tmp) do (
    set foo=%%a
    if "!foo!"=="<"ex3">" set "foo=<"ex5">"
    echo !foo! >> text.file) 
del text.tmp