首页 > 解决方案 > 为什么我的批处理脚本不能正确替换?

问题描述

我有这个脚本来替换文本文件中的一些单词。不知道对不对:

@echo off
setlocal EnableDelayedExpansion
set count=0
set j=0
set i=0

for /f "tokens=* delims=" %%c in ( '"type test.txt"') do (
    SET string=%%b
    echo %%c >>test2.txt
)

for /F %%a in (params.txt) do (
    set array[!i!]=%%a
    set /A i+=1
)
for /F "tokens=1 delims==" %%p in (params.txt) do (
    set array2[!j!]=%%p
    set /A j+=1
)
set /A i-=1
for /l %%r in (%count% , 1, %i%) do (
   set replace=!array2[%%r]!
   echo %repace%
   set replaced=!array[%%r]!
   echo replaced
   set /A count+=1
   call :reemplaza
)


:reemplaza

if %count% LSS %i% ( 
    for /f "tokens=* delims=" %%b in ( '"type test2.txt"') do (
        SET string=%%b
        SET modified=!string:%replace%=%replaced%!s
        echo.!modified! >>auxiliar.txt
    )
    pause
    del test2.txt
    ren auxiliar.txt test2.txt
)

第一个循环是正确的,但在第二个循环中这样做:

我想替换:name=awachens for name=awachenss

所以取下一个参数并打印:

姓名=awachens=姓=大声笑

问候 !

标签: arraysloopsfiletextreplace

解决方案


推荐阅读