替换文件中字符串的bat脚本

时间:2021-08-26 09:02:26

将E:\strace\pmdebug\a.txt中的字符串%i替换成%replacestring%


sub.bat:

@echo off 

echo para1=%1 para2=%2 para3=%3 para4=%4
SetLocal EnableDelayedExpansion
for /f "delims=" %%i in (%1) do (
    set var=%%i
	set var=!var:%2=%3!
	echo !var!>>%4
)
rem SETLOCAL DisableDelayedExpansion

@echo on


main.bat:

SetLocal EnableDelayedExpansion

set replacestring=1

for /l %%i in (1,1,5) do (
    set /a replacestring+=1
	echo !replacestring!
    call sub.bat E:\strace\pmdebug\a.txt %%i !replacestring! E:\strace\pmdebug\b.txt
	pause
	del a.txt
	ren b.txt a.txt
)
pause