首页 > 解决方案 > 替换 TXT 文件中的字符串

问题描述

我想替换文本文件中的一行。下面的脚本用于删除该行,但它将该行替换为 true==true。我知道这是由于我的>>"%textFile%" echo(!line:%search%=%!)设置方式。我希望它只是删除该行,然后我可以回显替换变量,但我在弄清楚如何做到这一点时遇到了问题。

@echo off &setlocal
set "search=xxxxx"
set "replace=yyyyy"
set "search_1=zzzzz"
set "replace_1=aaaaa"
set "textfile=%userprofile%\AppData\....."
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    >>"%textFile%" echo(!line:%search%=%!)
>>"%textFile%" echo(%replace%
endlocal
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    >>"%textFile%" echo(!line:%search_1%=%!)
>>"%textFile%" echo(%replace_1%
endlocal
)

标签: batch-filereplacecmd

解决方案


推荐阅读