我有一个文本文件test.txt,文字为
=============================================== ==
PATH=C:\ProgramFiles\Think\Digit\Trunk.exe
WORD=C:\ProgramFiles\Microsoft\Word12\Word.exe
=============================================== ==
我可以替换文本" C:\ ProgramFiles \ Think \ Digit \ Trunk.exe"用这个词,"删除" ..........
但是,我想通过搜索来替换整行............ PATH = C:\ ProgramFiles \ Think \ Digit \ Trunk.exe .......使用变量SEARCHTEXT。但是,我无法使用' ='操作
=============================================== ===================================
@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=C:\Users\PrasanthReddy\Desktop\test.txt
set OUTTEXTFILE=C:\Users\PrasanthReddy\Desktop\testsimple.txt
set SEARCHTEXT=C:\ProgramFiles\Think\Digit\Trunk.exe
set REPLACETEXT=Deleted
set OUTPUTLINE=
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if "!string!" == "" (
echo.>>%OUTTEXTFILE%
) else (
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
=============================================== ============================
任何人都可以帮我改变它......我怎么能这样做....
答案 0 :(得分:1)
不是输出具有替换值的字符串,而是确定替换是否已完成,并根据结果回显已确定的值
@echo off
setlocal enableextensions disabledelayedexpansion
set "INTEXTFILE=C:\Users\PrasanthReddy\Desktop\test.txt"
set "OUTTEXTFILE=C:\Users\PrasanthReddy\Desktop\testsimple.txt"
set "SEARCHTEXT=C:\ProgramFiles\Think\Digit\Trunk.exe"
set "REPLACETEXT=Deleted"
( for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%INTEXTFILE%"'
) do if "%%b"=="" (
echo(
) else (
set "string=%%b"
setlocal enabledelayedexpansion
if "!string:%SEARCHTEXT%=!"=="!string!" (
endlocal
echo(%%b
) else (
endlocal
echo(%REPLACETEXT%
)
)
) > "%OUTTEXTFILE%"
move /y "%OUTTEXTFILE%" "%INTEXTFILE%"